我正在尝试使用docker postgres图像运行我的第一个飞行路线示例,但出现以下错误:
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容器,可以看到在docker-compose执行过程中创建了用户角色flyway-service:
$ 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-compose yml文件:
version: "3.8"
services:
postgres:
image: postgres:12.2
ports: ["5432:5432"]
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=flyway-service
我在MAC OSX上运行此代码。我想,我这里缺少明显的东西,但不确定是什么!任何指针将不胜感激。
最佳答案
终于在 friend 的帮助下找到了问题所在!问题不在于附加的代码,而是旧的Postgres安装在同一端口5432
上运行的postgres守护进程。
我找到了完整的卸载过程here。删除其他守护程序后,仅监听了一个端口。
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)
关于postgresql - Flyway无法在Postgres Docker中找到角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61856828/