Postgres Docker image is not creating database with custom name

Providing the environment variables, as you did, SHOULD create the customname database when the container is initialized. There is no need to create the username and database in the /docker-entrypoint-initdb.d/' init scripts.

I would make sure there isn't any hanging postgres_data volume. If you have previously started the container without specifing the environment variables, the volume gets created for the default postgres database. Next time you start the container (with the POSTGRES_DB env specified), the database creation part is skipped.

Just to make sure, remove any created volume (the name should be something like *_postgres_data)

docker volume ls
docker volume rm <volume_name>

See User and DB were not created from environment variable arguments as well. Hope that helps


If you've changed the username/password since the very first run, try to delete the prior volume created

    docker volume rm <volume-name>

Then run the compose file again


You need to create the database first.

If you want to do that automatically for new data directories, then the official Docker Postgres image has an option to do so by placing Initialization Scripts with the extension .sql in the /docker-entrypoint-initdb.d/ directory.

For example, create a file with contents like:

CREATE USER custom_user;
CREATE DATABASE custom_db;
GRANT ALL PRIVILEGES ON DATABASE custom_db TO custom_user;

And save it to /docker-entrypoint-initdb.d/create-db.sql in the container, e.g. with COPY in the Dockerfile. Scripts with extension .sql inside that directory will only run if the DATA directory is empty, and multiple files will run in the alphabetical order of the file names.

If you want to set it up manually, you can also do that with the createdb utility

createdb [connection-option...] [option...] [dbname [description]]

Or by connecting to the postgres database and use the CREATE DATABASE ... command, e.g.

docker exec -it store psql -U postgres -c 'CREATE DATABASE customname;'

If you connect interactively as in your question, you can do the following:

$ docker exec -it store psql -U postgres
psql (11.3)
Type help for help.

postgres=# CREATE DATABASE customname;
CREATE DATABASE

postgres=# \c customname

The last command will connect you to the customname database.