Postgres 16 compatibility (FATAL: database does not exist)
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.
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.
Thanks for the suggestion. Glad to hear this helped you resolve the issue you had.