首页 > 解决方案 > 使用 data.sql 文件初始化 postgreSQL db 并进行 utf-8 编码工作

问题描述

当使用 data.sql 文件通过 application.properties 初始化 postgreSQL 数据库时,插入的带有需要 utf-8 编码的字符(如 Ä 和 ö)的数据不会在各自的字段中正确编码。

所以,我在 application.properties 文件中有以下内容来填充数据库......

spring.datasource.initialization-mode=always
spring.datasource.data=classpath:data.sql

...

Spring 通过以下方式创建模式:

spring.jpa.hibernate.ddl-auto=create-drop

我还应该定义什么来让这些字符正确编码 utf-8。

此外,在 pgAdmin 属性中,我可以看到数据库本身是 utf-8 编码的。

标签: postgresqlhibernatespring-bootspring-data-jpa

解决方案


我从另一个线程中找到了答案:Spring and PostgreSQL 之间的错误编码

这只是在应用程序属性中所需要的,而 utf-8 编码现在可以工作:

spring.datasource.sql-script-encoding= UTF-8

推荐阅读