java项目mysql转postgresql

特殊函数 :

mysql:

find_in_set(?, ancestors)

postgresql:

? = ANY (string_to_array(ancestors,','))

mysql:

date_format(t1.oper_time, '%Y-%m-%d')

postgresql:

rksj::date

to_char(inDate,'YYYY-MM-DD')

mysql:

concat(t1.id, ifnull(t1.perms, ''))

postgresql:

concat(t1.id, COALESCE(t1.perms,''))

JDBC链接

按顺序访问public、sys、xxx数据库的表,表名不能重复,无需加前缀;

jdbc_url=jdbc:postgresql://127.127.0.1:5432/dbname?currentSchema=public,sys,xxx

仅访问sys库,若系统里面还需要访问xxx数据库,那么必须加前缀加个点 "xxx.";

jdbc_url=jdbc:postgresql://127.127.0.1:5432/dbname?currentSchema=sys

最后,mysql支持自动类型转换,postgresql是强类型数据库,特别注意数字与字符串在写SQL时候的区分;字符串必须带单引号!

注意,postgresql查询结果集的字段名称,只支持小写!可能会导致设置JavaBean不起作用!


1、使用navicat工具把mysql的数据库复制一份到postgresql中对应的数据库中

2、修改application-druid.yml的相应的参数

driverClassName: org.postgresql.Driver

url: jdbc:postgresql://localhost:5432/xx_xxx

validationQuery: SELECT 1

3、修改postgresql数据中qrtz_job_details表中varchar 1为varchar 10,否则运行会提示:对于可变字符类型来说,值太长了(1)

4、在ScheduleConfig中添加prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");

5、全局替换项目中使用的sysdate()函数为now(),因为postgresql数据库没有sysdate()函数

6、全局替换项目中使用的ifnull()函数为coalesce(),因为postgresql数据库没有ifnull()函数。注意只替换ifnull为coalesce,不带括号。

7、修改部门查询SQL中使用到的find_in_set函数,把 find_in_set(#{deptId}, ancestors) 替换cast(#{deptId} as varchar) = any(string_to_array(ancestors,','))

8、处理数据库中表的自增问题,mysql数据库支持直接对主键自增,postgresql数据库可更改MySQL主键自增auto_increment为PGSql的序列类型BIGSERIAL

也可通过建立序列的方式来实现对主键的自增,所以需要对所有主键自增的表进行序列创建和关联。完整可执行SQL

--创建用户表ID自增序列

create sequence public.sys_user_id_seq

increment 1

start 200

minvalue 1

maxvalue 99999999

cache 1;

--把序列和用户表ID关联上

alter table public.sys_user alter column user_id set default nextval('public.sys_user_id_seq');

9、修改Mapper中 status = 0 为 status = '0'

10、修改数据库中的默认值

如:sys_user中的user_type、sex、status、del_flag的默认值

11.sys_menu 表中的两个属性类型int4改为varchar

相关推荐
知其然亦知其所以然10 分钟前
面试被问 G1 GC 懵了?记住这几点就能完美回答!
java·后端·面试
能工智人小辰17 分钟前
二刷 黑马点评 秒杀优化
java·开发语言
杨小扩18 分钟前
夯实基础:配置Java开发环境JDK与构建工具Maven
java·开发语言·maven
开开心心_Every1 小时前
免费PDF文件格式转换工具
java·智能手机·pdf·word·batch·java-zookeeper
Kiri霧1 小时前
Kotlin集合分组
android·java·前端·kotlin
C++chaofan1 小时前
45. 跳跃游戏 II
java·开发语言·数据结构·算法·leetcode·游戏·职场和发展
艾特小小1 小时前
spring-cloud微服务部署-feign服务间调用
java·spring boot·spring cloud·微服务·架构
dingdingfish1 小时前
PostgreSQL 16 Administration Cookbook 读书笔记:第7章 Database Administration
postgresql·database·administration·cookbook
程序员的世界你不懂2 小时前
CentOS下安装Mysql
mysql·adb·centos
Cyanto2 小时前
SpringMVC @ResponseBody注解详解
java·开发语言