docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

the database is permanently (not where is stored) #1119

Closed JNaranjo-Alcazar closed 1 year ago

JNaranjo-Alcazar commented 1 year ago

Hi community,

I have implemented this docker-compose file:

version: '3.8'
services:
  db:
    container_name: container_postgres
    image: postgres:11.12
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "5432:5432"
    volumes:
      - ${PATH_TO_DATABASE}:/var/lib/postgresql/data
      - ./create_database.sql:/docker-entrypoint-initdb.d/create_tables.sql 

  pgadmin:
    container_name: container_pgadmin
    image: dpage/pgadmin4:5
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEAFULT_PASSWORD}
    ports:
      - "5050:80"
volumes:
  pg-data:
    driver: local

create_database.sql contains this line:

CREATE DATABASE mydatabase;

The problem is as follows:

I would like the database to be created every time I launch docker compose up.

What happens is that I delete the containers, I delete the shared folder (PATH_TO_DATABASE) and when I launch it again, I get the following error:

psql:/docker-entrypoint-initdb.d/create_tables.sql:2: ERROR: database "mydatabase" already exists

I don't know where I have the information stored in the host so that every time I do the up I find that the database exists. I thought that deleting the containers and the shared volume would be enough. But this is not the case, can anyone explain me what exactly is happening?

It is worth noting that the permissions that appear in the PATH_TO_DATABASE folder are systemd-coredump:root. In case this is useful

Best regards and thank you very much