0

I try to up several postgres databases by writing docker-compose file and create database when container is up. The problem is - that database is not created

I do not understand why it doesn't create. But if i try to up only one postgres it works. What i missed?

works well:

services:
  tenant_first:
    image: postgres
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data/
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=tenant_first
    ports:
      - 50005:5432

Doesn't work (not create database if more than one recording)

services:
  tenant_first:
    image: postgres
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data/
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=tenant_first
    ports:
      - 50005:5432

  tenant_second:
    image: postgres
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data/
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=tenant_second
    ports:
      - 50008:5432

I did it works adding volumes

volumes:
  pgdata:
  pgdata2:

1
  • I figured out - it because i used one volumes dir for all my services. If i separate it it works. Also i changed my docker-compose exclude POSTGRES_DB instead of i use script .sh Commented Oct 17, 2021 at 12:38

1 Answer 1

2

Here the full workable solution what i did

version: '3.3'

services:
  tenant_first:
    image: postgres
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data/
      - ./sql/initdb.sh:/docker-entrypoint-initdb.d/initdb.sh
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres

    ports:
      - 50005:5432
#
  tenant_fours:
    image: postgres
    restart: always
    volumes:
      - pgdata2:/var/lib/postgresql/data/
      - ./sql/initdb_f.sh:/docker-entrypoint-initdb.d/initdb_f.sh
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
    ports:
      - 50008:5432

networks:
  default:
    external:
      name: flask_mu_one_dbs

volumes:
  pgdata:
  pgdata2:

initdb.sh

#!/bin/bash
psql -U postgres
psql -c "create database tenant_first"

initdb_f.sh

#!/bin/bash
psql -U postgres
psql -c "create database tenant_fours"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.