1
votes

I am trying to run my first flyway example using docker postgres image but getting the following error:

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)

I looked up into the docker container and can see that the user role flyway-service is created as part of the docker-compose execution:

$ 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=# 

Main class is:

    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!" );
    }
}

The migration called 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 file:

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

I am running this code on MAC OSX. I assume, I am missing something obvious here, but not sure what! Any pointers would be appreciated.

1

1 Answers

1
votes

Finally managed to figure out the issue with the help of a friend! The problem was not with the attached code but with a postgres daemon process running on the same port 5432 by an old Postgres installation.

I found the complete uninstallation procedure here. After removing the additional daemon process got only one port listening.

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)