Back
Featured image of post FATAL Database Does Not Exist Docker Compose PostgreSQL

FATAL Database Does Not Exist Docker Compose PostgreSQL

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 of POSTGRES_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

Reference

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy