首页 > 解决方案 > Flyway 无法找到 postgres docker 的角色

问题描述

我正在尝试使用 docker postgres 图像运行我的第一个 flyway 示例,但出现以下错误:

INFO: Flyway Community Edition 6.4.2 by Redgate
Exception in thread "main" org.flywaydb.core.internal.exception.FlywaySqlException: 
Unable to obtain connection from database (jdbc:postgresql://localhost/flyway-service) for user 'flyway-service': FATAL: role "flyway-service" does not exist
-------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State  : 28000
Error Code : 0
Message    : FATAL: role "flyway-service" does not exist

    at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:65)
    at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80)

我查看了 docker 容器,可以看到用户角色 flyway-service 是作为 docker-compose 执行的一部分创建的:

$ docker exec -it flywayexample_postgres_1 bash
root@b2037e382112:/# psql -U flyway-service;
psql (12.2 (Debian 12.2-2.pgdg100+1))
Type "help" for help.

flyway-service=# \du;
                                      List of roles
   Role name    |                         Attributes                         | Member of 
----------------+------------------------------------------------------------+-----------
 flyway-service | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

flyway-service=# 

主要课程是:

    public static void main( String[] args ) {
        var flyway = Flyway.configure().schemas("flyway_test_schema")
                .dataSource("jdbc:postgresql://localhost/flyway-service", "flyway-service",
                        "password")
                .load()
                .migrate();
        System.out.println( "Flyway example's hello world!" );
    }
}

迁移称为 src/main/resources/db/migration/V1__Create_person_table.sql:

create table PERSON (
    ID int not null,
    NAME varchar(100) not null
);

Docker 编写 yml 文件:

version: "3.8"
services:
  postgres:
    image: postgres:12.2
    ports: ["5432:5432"]
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=flyway-service

我在 MAC OSX 上运行此代码。我想,我在这里遗漏了一些明显的东西,但不确定是什么!任何指针将不胜感激。

标签: postgresqldockerflyway

解决方案


终于在朋友的帮助下解决了这个问题!5432问题不在于附加代码,而在于旧 Postgres 安装在同一端口上运行的 postgres 守护进程。

我在这里找到了完整的卸载程序。删除额外的守护进程后,只有一个端口监听。

a$ lsof -n -i4TCP:5432
COMMAND   PID         USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
com.docke 654 root   50u  IPv6 0x7ae1b5f8fbcf1cb      0t0  TCP *:postgresql (LISTEN)

推荐阅读