[使用Docker连接到Postgres

问题描述 投票:0回答:2

我有点迷路。我有一个运行在Docker容器中的postgres数据库,该数据库开始使用docker compose。一切都很好,直到最近我收到一个错误消息说

FATAL: password authentication failed for user "postgres"

我使用spring连接到数据库-jdbc。

application.properties

## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=password

Dockerfile

FROM library/postgres
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB mydb

docker-compose

version: '3'

services:
    database:
        build: ./postgres
        ports: 
            - "5432:5432"
        restart: unless-stopped

我可以使用pgAdmin连接到数据库,但是不能使用spring-application或SQuirreL进行连接。

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"(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)
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:525)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:146)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
    at org.postgresql.Driver.makeConnection(Driver.java:459)
    at org.postgresql.Driver.connect(Driver.java:261)
    at net.sourceforge.squirrel_sql.fw.sql.SQLDriverManager.getConnection(SQLDriverManager.java:147)
    at net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommand.executeConnect(OpenConnectionCommand.java:148)
    at net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommand.lambda$execute$0(OpenConnectionCommand.java:93)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:830)

postgresql jdbc
2个回答
0
投票

我可以连接到Docker容器内的Postgresql数据库。我想您应该在docker-compose文件中写入数据库名称。这是我在docker-compose中的db。另外,我将所有postgres confs写入了compose文件而不是Dockerfile中。然后我这样写了jdbc:postgresql:// db:5432 / abdu。在这里,而不是本地主机,我在撰写文件中写了数据库名称。并确保您正确输入数据库用户名和密码。希望对您有所帮助。

application.properties

spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.jdbc-url=jdbc:postgresql://db:5432/abdu

Dockerfile

FROM openjdk:8
ADD jarfolder/docker-time-zona.jar docker-time-zona.jar
EXPOSE 8086
ENTRYPOINT ["java", "-jar", "/docker-time-zona.jar"]
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

docker-compose.yml

version: '3'

services:
  web:
    build: .
  db:
    restart: always
    container_name: docker-postgresql
    image: postgres:9.6
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=postgres
      - POSTGRES_DB=abdu
      - PGDATA=/var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"

0
投票

好吧,我修复了它。我绝望地更改了用户名和密码。它奏效了,现在我比以前更加困惑

© www.soinside.com 2019 - 2024. All rights reserved.