使用docker安装postgres
简单起见,这里用docker来安装postgresql
docker pull postgres
docker run --name postgres \-e POSTGRES_PASSWORD=123456 \-p 5432:5432 \-v /usr/local/docker/postgresql/data:/var/lib/postgresql/data \-d postgres
postgres客户端
pgAdmin4镜像网站下载:https://mirrors.aliyun.com/postgresql/pgadmin/pgadmin4/
使用pgAdmin4创建表,手动创建即可
springboot整合postgres
持久层还用的mybatis,先导入下依赖
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId>
</dependency>
application.yml配置,这块,以前mysql是怎么配,postgres就怎么配,换个用户密码的事儿
spring:datasource:type: com.zaxxer.hikari.HikariDataSource#PostgreSQL驱动driver-class-name: org.postgresql.Driver#PostgreSQL配置url: jdbc:postgresql://192.168.133.128:5432/postgres?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf-8&useSSL=false#用户密码username: postgrespassword: 123456hikari:connection-timeout: 60000validation-timeout: 3000idle-timeout: 60000login-timeout: 5max-lifetime: 60000maximum-pool-size: 400minimum-idle: 100read-only: false
对postgresql增删改查
对postgresql增删改查代码和对mysql的增删改查没有任何区别,我们只是更换了数据源,代码不用做任何改动(比如说之前是通过mybatis操作mysql,现在只是把mysql换成postgre,代码还是保持原来一样即可)