Postgres数据库问题编码UTF-8

发布于 2025-02-07 21:45:26 字数 2021 浏览 1 评论 0原文

我已经使用此命令Docker创建了一个Docker映像-D 我能够在http:// localhost中加载pgadmin实例:5050/浏览器/ 在相同的情况下创建数据库和表,凭据正常工作。

但是,当我开始运行主要的Spring Boot应用程序 CustomerApplication 时,它会在以下错误&gt中失败。

org.postgresql.util.util.psqlexception:致命:laautentificación密码fallóparapara el usuario«amigoscode»(pgjdbc:自动检测服务器对编码为iso-88859-1 /或主机,端口,dbname,用户,密码,pg_hba.conf)

我不知道怎么了,我的凭据是正确的。 似乎是什么问题?

应用

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: amigoscode
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

server:
  port: 8080
spring:
  application:
    name: customer
  datasource:
    username: amigoscode
    url: jdbc:postgresql://localhost:5432/customer
    password: password
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true

以下 客户表脚本

CREATE DATABASE customer
    WITH
    OWNER = amigoscode
    ENCODING = 'UTF8'
    LC_COLLATE = 'en_US.utf8'
    LC_CTYPE = 'en_US.utf8'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;

I have created a docker image with this command docker compose up -d
where I was able to load pgAdmin instance in http://localhost:5050/browser/
create a database and table in the same , credentials are working properly.

However when I start to run my main spring boot application CustomerApplication it fails with below error >

org.postgresql.util.PSQLException: FATAL: la autentificación password falló para el usuario «amigoscode» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf)

I do not know what is wrong, my credentials are correct.
what seems to be the issue?

below are application.yml and docker-compose.yml

docker-compose.yml

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: amigoscode
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

application.yml

server:
  port: 8080
spring:
  application:
    name: customer
  datasource:
    username: amigoscode
    url: jdbc:postgresql://localhost:5432/customer
    password: password
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true

customer table Script

CREATE DATABASE customer
    WITH
    OWNER = amigoscode
    ENCODING = 'UTF8'
    LC_COLLATE = 'en_US.utf8'
    LC_CTYPE = 'en_US.utf8'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2025-02-14 21:45:26

由于我在本地(port 5432 )中安装了Postgres实例并运行,因此MicroService客户试图连接到该实例,而不是使用同一端口的Docker中的一个。

解决方案是从 application.yml 更改URL端口
URL:JDBC:PostgreSql:// localhost: 5433 /客户

ports的Postgres港口

以及来自 docker-compose.yml


- “ 5432 :5432”

to

端口:
- “ 5433 :5432”,

因此微服务连接到docker映像中的postgres实例,而不是本地的,

然后重新运行的docker命令docker docker构成-d
运行客户应用 sprinbgootapplication ),这次
通过创建客户表,应用程序启动良好,顺利。

Since I have a postgres instance installed and running in my local (port 5432), the microservice customer was trying to connect to that instance, not the one from docker which was using the same port.

the solution was to change the url port from application.yml
url: jdbc:postgresql://localhost:5433/customer

and the port of postgres from docker-compose.yml

from

ports:
- "5432:5432"

to

ports:
- "5433:5432"

so microservice connects to the postgres instance in the docker image, not the local one

then re-run docker command docker compose up -d
run the CustomerApplication (SprinbgootApplication) and this time
application starts up nice and smoothly by creating the customer table.

enter image description here

白芷 2025-02-14 21:45:26

当您创建角色Amigoscode时,您是否为其设置了密码?

CREATE ROLE amigoscode WITH LOGIN PASSWORD 'password';

您可能已经登录pgadmin作为postgres superuser,创建了角色amigoscode,并且从未设置密码。编码问题看起来类似于指定的在这里提供的密码不正确。看起来PGJDBC错误是在2014年修复的,但可能已经在更现代的版本中进行了回归 - for-postgresql-messages in-jdbc“>类似的未解决的问题。

其他可能的替代方法:

  • Spring_datasource_password环境变量设置为其他。
  • 您还拥有一个application.properties文件,该文件具有不同的密码,该密码将优先于application.yml
  • 您在应用程序之前检查的任何属性来源中指定了不同的密码。

When you created the role amigoscode did you actually set a password for it?

CREATE ROLE amigoscode WITH LOGIN PASSWORD 'password';

You may have logged into pgadmin as the postgres superuser, created the role amigoscode and never set a password. The encoding issue looks similar to the one specified here which appears when the supplied password is incorrect. It looks like that pgjdbc bug was fixed in 2014, but has possibly regressed in more modern versions >= 42.2.x as there is currently a similar unresolved issue.

Other likely alternatives:

  • SPRING_DATASOURCE_PASSWORD environment variable is set to something else.
  • You also have an application.properties file which has a different password that will take precedence over the one in application.yml.
  • You have a different password specified in any of the property sources that spring checks before application.yml (discussion of precedence here)
翻了热茶 2025-02-14 21:45:26

我也发生了同样的事情,问题是我正在运行另一个正在使用端口5432(PostgreSql Server和Docker)的实例,我正在使用DBEAVER,它试图连接到该实例,而是我想使用的实例。

检查哪些应用程序正在使用端口并关闭您不需要的应用程序,然后重新尝试连接到数据库。

The same happened to me, the problem is that i was running another instance that was using the port 5432 (postgresql server and docker), I was using dbeaver and it was trying to connect to that instance instead the one that i wanted to use.

Check which applications are using the port and close the ones that you don't need and try again to connect to the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文