尽量不踩坑配置postgresql

首先放参考链接:
https://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html
https://www.runoob.com/manual/PostgreSQL/index.html
http://www.wfuyu.com/datay/21064.html

我的开发环境不能直接连接外网的,所以我使用源码包安装的方式,推荐使用yum方式。
源码包地址如下,根据自己centOS版本选择。
http://ftp.postgresql.org/pub/source/

解压源码包
tar xjf postgresql-9.2.4.tar.bz2
进入解压后的目录
cd postgresql-9.2.4

接着放简略版
依次执行就完事了
./configure
make
su
make install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test

安装完成后,开放访问ip段!!不然绝不可能连得上!
在远程的postgresql数据库上的pg_hba.conf中添加 host all all 0.0.0.0/0 trust
并保证postgresql.conf 中参数设置为listen_addresses = ‘*’

在这里插入图片描述
在这里插入图片描述
开发环境就这么用没问题!!!
生产环境根据服务器ip修改号码段!!!

如果postgresql启动失败,在 /usr/local/pgsql/data的serverlog里面查看,/data目录是自己定义的,所以也可以选择你认为合适的目录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.5</version> </dependency> ``` 2. 配置数据源 在 application.properties 文件中添加以下配置: ``` spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/dbname spring.datasource.username=username spring.datasource.password=password ``` 其中,dbname 为数据库名称,username 和 password 分别为数据库的用户名和密码。 3. 配置 JPA 如果使用 JPA 来操作数据库,还需要在 application.properties 文件中添加以下配置: ``` spring.jpa.database=POSTGRESQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect ``` 其中,spring.jpa.hibernate.ddl-auto=update 表示在启动应用程序时自动更新数据库结构。 4. 测试连接 在代码中使用 @Autowired 注解注入 DataSource 对象,并调用 getConnection() 方法测试连接是否成功。 ``` @Autowired private DataSource dataSource; public void testConnection() { try { Connection connection = dataSource.getConnection(); System.out.println("连接成功"); } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); } } ``` 以上就是使用 Spring Boot 配置 PostgreSQL 数据库的步骤。 ### 回答2: Spring Boot 是一个开源的 Java 开发框架,它简化了基于 Spring 框架的应用程序的配置和部署。下面是使用 Spring Boot 配置 PostgreSQL 数据库的步骤: 步骤1:添加 PostgreSQL 依赖 首先,在 Spring Boot 项目的 pom.xml 文件中添加 PostgreSQL 依赖。可以使用以下代码添加依赖: <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>版本号</version> </dependency> 请将 "版本号" 替换为您想要使用的 PostgreSQL 版本号。 步骤2:配置数据库连接 在项目的配置文件(例如 application.properties 或 application.yml)中,添加以下属性来配置数据库连接: spring.datasource.url=jdbc:postgresql://localhost:5432/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=org.postgresql.Driver 请将 "your_database_name"、 "your_username" 和 "your_password" 替换为您的 PostgreSQL 数据库的名称、用户名和密码。 步骤3:启用自动配置 在项目的启动类上添加 @EnableAutoConfiguration 注解,以启用 Spring Boot 的自动配置功能。 步骤4:编写数据库操作代码 根据您的业务需求,编写与数据库交互的代码。可以使用 Spring Data JPA 或 JDBC 等技术来简化数据库操作。 以上是使用 Spring Boot 配置 PostgreSQL 数据库的简单步骤。通过这些配置,您可以方便地在 Spring Boot 项目中使用 PostgreSQL 数据库进行开发。 ### 回答3: Spring Boot是一个快速构建基于Spring框架的应用程序的工具。要配置Spring Boot与PostgreSQL数据库连接,需要完成以下步骤: 1. 引入PostgreSQL依赖:在项目的pom.xml文件中添加PostgreSQL依赖项,例如: ```xml <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>版本号</version> </dependency> ``` 2. 配置数据库连接信息:在application.properties或application.yml文件中添加数据库连接信息,包括数据库URL、用户名、密码等。例如: ```properties spring.datasource.url=jdbc:postgresql://localhost:5432/db_name spring.datasource.username=username spring.datasource.password=password spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect ``` 3. 创建数据源bean:在Spring Boot的配置类中创建一个DataSource bean,该bean将使用上述配置的数据库连接信息。例如: ```java @Configuration public class DataSourceConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() { return DataSourceBuilder .create() .url(url) .username(username) .password(password) .build(); } } ``` 4. 创建JdbcTemplate bean:在Spring Boot的配置类中创建一个JdbcTemplate bean,用于执行数据库操作。例如: ```java @Configuration public class JdbcTemplateConfig { @Autowired private DataSource dataSource; @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource); } } ``` 通过以上步骤,我们成功配置了Spring Boot与PostgreSQL数据库的连接。可以使用JdbcTemplate或其他适合的方式访问数据库进行数据操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值