In some case I failed to create database name using postgreSQL in docker here is a sample docker compose for database configuration
version: "3.9"
services:
db:
container_name: "db"
image: postgres:13
env_file:
- ./.env.prod
ports:
- ${DB_PORT}:${DB_PORT}
volumes:
- data:/var/lib/postgresql/data
environment:
- POSTGRES_NAME=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PWD}
networks:
- oveda-network
and I got an
Error FATAL: database does not exist
because I not define POSTGRES_DB
so default database name will be POSTGRES_USER
value.
based discussion on Github
POSTGRES_DB
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value ofPOSTGRES_USER
will be used.
so your .env
must like
DB_NAME=user
DB_USER=user
if you not define POSTGRES_DB
in your docker-compose
for example if you define on your compose
version: "3.9"
services:
db:
container_name: "db"
image: postgres:13
env_file:
- ./.env.prod
ports:
- ${DB_PORT}:${DB_PORT}
volumes:
- data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PWD}
- POSTGRES_DB=${DB_NAME}
networks:
- oveda-network