Docker Community Forums

Share and learn in the Docker community.

Postgres inside container INSIST to look for DATABASE with same name of USER

Hey Experts … maybe some of you can help here with POSTGRESQL + DOCKER
Sorry if I select WRONG category … FIRST TIME HERE

I have build a Container with Postgres and setup custom database name and database user

Create Start the server but it seems do not create DATABASE .

I can see

Creating autopwaapi_autoapidb_1 ... done

Attaching to autopwaapi_autoapidb_1

autoapidb_1 | The files belonging to this database system will be owned by user "postgres".

autoapidb_1 | This user must also own the server process.

autoapidb_1 |

autoapidb_1 | The database cluster will be initialized with locale "en_US.utf8".

autoapidb_1 | The default database encoding has accordingly been set to "UTF8".

autoapidb_1 | The default text search configuration will be set to "english".

autoapidb_1 |

autoapidb_1 | Data page checksums are disabled.

autoapidb_1 |
autoapidb_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok

autoapidb_1 | creating subdirectories ... ok

autoapidb_1 | selecting dynamic shared memory implementation ... posix

autoapidb_1 | selecting default max_connections ... 100

autoapidb_1 | selecting default shared_buffers ... 128MB

autoapidb_1 | selecting default time zone ... Etc/UTC

autoapidb_1 | creating configuration files ... ok

autoapidb_1 | running bootstrap script ... ok

autoapidb_1 | performing post-bootstrap initialization ... ok

autoapidb_1 | syncing data to disk ... ok

autoapidb_1 |

autoapidb_1 |

autoapidb_1 | Success. You can now start the database server using:

autoapidb_1 |

autoapidb_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start

autoapidb_1 |

autoapidb_1 | initdb: warning: enabling "trust" authentication for local connections
autoapidb_1 | You can change this by editing pg_hba.conf or using the option -A, or

autoapidb_1 | --auth-local and --auth-host, the next time you run initdb.

autoapidb_1 | waiting for server to start....2021-05-20 13:27:47.453 UTC [48] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

autoapidb_1 | 2021-05-20 13:27:47.457 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

autoapidb_1 | 2021-05-20 13:27:47.472 UTC [49] LOG: database system was shut down at 2021-05-20 13:27:46 UTC

autoapidb_1 | 2021-05-20 13:27:47.481 UTC [48] LOG: database system is ready to accept connections

autoapidb_1 | done

autoapidb_1 | server started

autoapidb_1 | CREATE DATABASE

autoapidb_1 |

autoapidb_1 |

autoapidb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/create-db.sql

autoapidb_1 | 2021-05-20 13:27:48.071 UTC [83] ERROR: role "autoapi" already exists

autoapidb_1 | 2021-05-20 13:27:48.071 UTC [83] STATEMENT: CREATE USER autoapi;
autoapidb_1 | psql:/docker-entrypoint-initdb.d/create-db.sql:1: ERROR: role "autoapi" already exists

autopwaapi_autoapidb_1 exited with code 3

It is not clear to me how he see ROLE AUTOAPI already exists as it was the first startup of container

My create-db.sql is like below :

CREATE USER autoapi;
CREATE DATABASE autoapidb;
GRANT ALL PRIVILEGES ON DATABASE autoapidb TO autoapi;

Should I do some kind of tests to avoid abort on CREATE USER error ?

Here are my config files

docker-compose.yml

version: '3.8'
services:
autoapidb:
build:
context: .
dockerfile: Dockerfile.pg
env_file:
- ./.env
volumes:
- pg_data:/var/lib/postgresql/data
- ./db/create-db.sql:/docker-entrypoint-initdb.d/create-db.sql
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "80:3000"
volumes:
- .:/usr/src/autopwaapi

redis:
image: redis
volumes:
gem_cache:
pg_data:
node_modules:

Dockerfile.pg

FROM postgres
ENV POSTGRES_DB autoapidb
ENV POSTGRES_USER autoapi
ENV POSTGRES_PASSWORD Postgres2021
COPY ./db/create-db.sql /docker-entrypoint-initdb.d/create-db.sql

.env

# DB

# Parameters used by db container

POSTGRES_DB=autoapidb

POSTGRES_USER=autoapi

POSTGRES_PASSWORD=Postgres2021