Problem with create database in docker-compose / postgres

Viewed 28

This is my docker-compose.yml

version: '3.8'
services:
  db:
    container_name: "postgres"
    image: postgres:14
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB:database
    ports:
      -   "127.0.0.1:5434:5432"
    volumes:
      - ./db:/var/lib/postgresql/data

Docker don't create database for this container, what is wrong?

1 Answers

IF IT IS NOT AN ISSUE WITH TYPO here - POSTGRES_DB:database instead of - POSTGRES_DB=database

Did you ever start the Postgres container without specifying the user and database?

This could happen when the docker volume had already stored data from previous runs, where you didn't set the user and DB, then later runs with different env variables will not change the users. That is only done on the first initialization.

So you need to run

docker volume prune

There is already a closed issue on GitHub about that, see: https://github.com/docker-library/postgres/issues/453

Related