Docker Compose 无法从数据库 (jdbc:postgresql://db:5432/postgres) 为用户“postgres”获取连接:连接尝试失败

Posted

技术标签:

【中文标题】Docker Compose 无法从数据库 (jdbc:postgresql://db:5432/postgres) 为用户“postgres”获取连接:连接尝试失败【英文标题】:Docker Compose Unable to obtain connection from database (jdbc:postgresql://db:5432/postgres) for user 'postgres': The connection attempt failed 【发布时间】:2021-08-15 07:30:52 【问题描述】:

问题:

我正在使用 Docker Compose 创建两个容器:一个带有 Postgres 数据库,另一个带有 Flyway。目标是使用 Flyway 将脚本迁移到 Postgres 数据库实例。当我运行 docker-compose up 时,出现以下错误:

Unable to obtain connection from database (jdbc:postgresql://db:5432/) for user 'luke_skywalker': The connection attempt failed.

我的代码如下,感谢您的帮助!

这是我的 docker-compose.yml:

version: "3.7"

services:
  postgres_db:
    image: postgres:11.3
    container_name: postgres_database
    restart: always
    # map host port to default postgres port of the contianer; note host port was randomly chose from ports that are unassinged
    environment:
      POSTGRES_USER: $postgres_user
      POSTGRES_PASSWORD: $postgres_password
    ports:
      - 11102:5432

  flyway_migration:
    #https://github.com/flyway/flyway-docker
    image: flyway/flyway
    container_name: flyway
    environment:
      FLYWAY_PASSWORD: $postgres_password
      FLYWAY_USER:  $postgres_user
    volumes:
        #create flyway supported volume in the container that has our migration scripts; https://github.com/flyway/flyway-docker
      - .\migration_scripts:/flyway/sql
    #change back to 60
    command: -url=jdbc:postgresql://db:5432/ -connectRetries=1 migrate -X
    depends_on:
      - postgres_db

这是完整的错误信息:

Attaching to postgres_database, flyway
postgres_database   | 2021-05-27 03:23:59.359 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_database   | 2021-05-27 03:23:59.359 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_database   | 2021-05-27 03:23:59.365 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_database   | 2021-05-27 03:23:59.385 UTC [23] LOG:  database system was shut down at 2021-05-27 03:19:05 UTC
postgres_database   | 2021-05-27 03:23:59.389 UTC [1] LOG:  database system is ready to accept connections
flyway              | DEBUG: Loading config file: /flyway/conf/flyway.conf
flyway              | DEBUG: Unable to load config file: /flyway/flyway.conf
flyway              | DEBUG: Unable to load config file: /flyway/flyway.conf
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jna-4.5.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/msal4j-1.10.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mssql-jdbc-9.2.1.jre8.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jna-platform-4.5.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/postgresql-42.2.19.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/hsqldb-2.5.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbyclient-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mysql-connector-java-8.0.24.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbytools-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jaybird-jdk18-3.0.10.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/google-cloud-spanner-jdbc-2.0.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/aws-secretsmanager-jdbc-1.0.6.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbyshared-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/snowflake-jdbc-3.13.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jtds-1.3.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derby-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mariadb-java-client-2.7.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/google-cloud-storage-1.113.13.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/sqlite-jdbc-3.34.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/ojdbc8-19.6.0.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/h2-1.4.200.jar
flyway              | DEBUG: Using configuration:
flyway              | DEBUG: flyway.connectRetries -> 1
flyway              | DEBUG: flyway.jarDirs -> /flyway/jars
flyway              | DEBUG: flyway.locations -> filesystem:sql
flyway              | DEBUG: flyway.password -> *************
flyway              | DEBUG: flyway.url -> jdbc:postgresql://db:5432/postgres
flyway              | DEBUG: flyway.user -> luke_skywalker
flyway              | DEBUG: Multiple databases found that handle url 'jdbc:postgresql://db:5432/postgres': CockroachDB, PostgreSQL
flyway              | DEBUG: Scanning for classpath resources at 'classpath:db/callback' ...
flyway              | DEBUG: Determining location urls for classpath:db/callback using ClassLoader java.net.URLClassLoader@18be83e4 ...
flyway              | DEBUG: Unable to resolve location classpath:db/callback.
flyway              | Flyway Community Edition 7.9.1 by Redgate
flyway              | DEBUG: AWS SDK available: false
flyway              | DEBUG: Google Cloud Storage available: false
flyway              | DEBUG: Scanning for filesystem resources at 'sql'
flyway              | DEBUG: Scanning for resources in path: sql (sql)
flyway              | DEBUG: Found filesystem resource: sql/V1__initial.sql
flyway              | WARNING: Connection error: The connection attempt failed. (Caused by db) Retrying in 1 sec...
flyway              | ERROR: Unexpected error
flyway              | org.flywaydb.core.internal.exception.FlywaySqlException:
flyway              | Unable to obtain connection from database (jdbc:postgresql://db:5432/postgres) for user 'luke_skywalker': The connection attempt failed.
flyway              | ----------------------------------------------------------------------------------------------------------------------------------------
flyway              | SQL State  : 08001
flyway              | Error Code : 0
flyway              | Message    : The connection attempt failed.
flyway              |
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:68)
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:68)
flyway              |   at org.flywaydb.core.Flyway.execute(Flyway.java:510)
flyway              |   at org.flywaydb.core.Flyway.migrate(Flyway.java:170)
flyway              |   at org.flywaydb.commandline.Main.executeOperation(Main.java:227)
flyway              |   at org.flywaydb.commandline.Main.main(Main.java:148)
flyway              | Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
flyway              |   at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
flyway              |   at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
flyway              |   at org.postgresql.Driver.makeConnection(Driver.java:465)
flyway              |   at org.postgresql.Driver.connect(Driver.java:264)
flyway              |   at org.flywaydb.core.internal.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:263)
flyway              |   at org.flywaydb.core.internal.jdbc.DriverDataSource.getConnection(DriverDataSource.java:227)
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:54)
flyway              |   ... 5 more
flyway              | Caused by: java.net.UnknownHostException: db
flyway              |   at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
flyway              |   at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
flyway              |   at java.base/java.net.Socket.connect(Unknown Source)
flyway              |   at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
flyway              |   at org.postgresql.core.PGStream.<init>(PGStream.java:95)
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
flyway              |   ... 12 more
flyway exited with code 1

【问题讨论】:

【参考方案1】:

正如异常消息所说:

Caused by: java.net.UnknownHostException: db [...]

您尝试通过db 主机连接到 postgres:

-url=jdbc:postgresql://db:5432/ -connectRetries=1 migrate -X

但是您将 postgres 别名设置为 postgres_db。只需将 flyway 的连接 url 更改为:

-url=jdbc:postgresql://postgres_db:5432/ -connectRetries=1 migrate -X

【讨论】:

哎呀,太傻了,我错过了。谢谢!

以上是关于Docker Compose 无法从数据库 (jdbc:postgresql://db:5432/postgres) 为用户“postgres”获取连接:连接尝试失败的主要内容,如果未能解决你的问题,请参考以下文章

Docker Compose - Flyway - 无法从数据源获取 Jdbc 连接

Docker Compose 无法连接到数据库

无法从 docker-compose 连接到 docker

Docker compose api无法连接到主机MongoDB数据库

Docker Compose 无法从数据库 (jdbc:postgresql://db:5432/postgres) 为用户“postgres”获取连接:连接尝试失败

无法从 docker-compose 文件中定义的另一个服务连接到 nsqd