docker-postgresql-multiple-databases icon indicating copy to clipboard operation
docker-postgresql-multiple-databases copied to clipboard

Postgres 16 compatibility (FATAL: database does not exist)

Open juhi24 opened this issue 1 year ago • 2 comments

Using a reasonably new version of postgres image (tried 16-alpine), you would get a

FATAL:  database "myapp" does not exist

See https://dev.to/nietzscheson/multiples-postgres-databases-in-one-service-with-docker-compose-4fdf#comment-2fd1m for further discussion and a possible workaround.

juhi24 avatar Aug 05 '24 10:08 juhi24

genuinely thank you so much for this, I had been stuck troubleshooting this problem. I would recommend to also put the error on the title for better visibility, I did not realize this was the whole culprit after all.

for those that only needed to create specific database(s)/user(s) (with passwords) and is able to mount the script to docker-entrypoint-initdb.d/:

#!/bin/bash

set -e
set -u

echo "setting up";

PGPASSWORD="${POSTGRES_PASSWORD}" psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE USER ... PASSWORD '...';
    CREATE DATABASE ...;
    GRANT ALL PRIVILEGES ON DATABASE ... TO ...;

    -- you can copy paste multiple of these commands if you need to
EOSQL

echo "setup done";

this setup requires to set POSTGRES_PASSWORD, POSTGRES_USER and POSTGRES_DATABASE. works on postgresql 16.

iyxan23 avatar Oct 12 '24 17:10 iyxan23

Thanks for the suggestion. Glad to hear this helped you resolve the issue you had.

juhi24 avatar Oct 14 '24 08:10 juhi24