How to modify docker settings for multiple database for a django project?

Viewed 170

My development file is as follows:

version: '3'

services:
  db:
    image: mdillon/postgis
    container_name: sl_database
    ports:
      - "5432:5432"
    volumes:
      - db_vesatogo_starling_v1:/var/lib/postgresql/data
    env_file:
      - ./config/dev.env
    networks: 
      - db_network

  db-replica:
    image: mdillon/postgis
    container_name: sl_database_replica
    ports:
      - "5433:5432"
    volumes:
      - db_vesatogo_starling_v1_replica:/var/lib/postgresql/data
    env_file:
      - ./config/dev1.env
    networks: 
      - db_network

  pgadmin:
    image: dpage/pgadmin4:4.28
    container_name: sl_admin
    links:
      - db
      - db-replica
    depends_on:
      - db
      - db-replica
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: password
    ports:
      - "80:80"
      - "81:80"
    networks: 
      - db_network

networks:
  db_network:
    driver: bridge

volumes:
  db_vesatogo_starling_v1:
  db_vesatogo_starling_v1_replica:
  static:

Now, I have introduced a replica database in my settings. My database settings is as follows:

DATABASES = {
    "default": {
        "ENGINE": "django_tenants.postgresql_backend",
        "NAME": os.environ.get("POSTGRES_DB", "starling"),
        "USER": os.environ.get("POSTGRES_USER", "user"),
        "PASSWORD": os.environ.get("POSTGRES_PASSWORD", "password"),
        "HOST": os.environ.get("POSTGRES_HOST", "db") if VT_ENV in PROD_ENV_TAGS else "localhost",
        "PORT": os.environ.get("POSTGRES_PORT", 5432),
        "TEST": {"NAME": os.environ.get("POSTGRES_TEST_DB", "starling_test")},
    },
    "replica": {
        "ENGINE": "django_tenants.postgresql_backend",
        "NAME": os.environ.get("POSTGRES_DB", "starling_replica"),
        "USER": os.environ.get("POSTGRES_USER", "user"),
        "PASSWORD": os.environ.get("POSTGRES_PASSWORD", "password"),
        "HOST": os.environ.get("POSTGRES_HOST", "db") if VT_ENV in PROD_ENV_TAGS else "localhost",
        "PORT": os.environ.get("POSTGRES_PORT", 5433),
        "TEST": {"NAME": os.environ.get("POSTGRES_TEST_DB", "starling_test")},
    },
}

After these configuration, when I am running a query (eg: Table.objects.using("replica").all()), I am getting following error: django.db.utils.ProgrammingError: column COLUMN_NAME does not exist. And yes, I have done all migrations correctly! Please suggest what changes should I make?

1 Answers
  1. Keep the service names different, like db:
  2. Use a separate host port, but keep the same container port "HOST_PORT:5432"
  3. Create different volumes for those replicas.

With these 3 points taken care of, I think you don't need to worry about anything.

This is extra: Your db and pgadmin services are on the same network, hence they can talk to each other. but redis services cannot be accessed by the other two services. To fix this add the redis_network to pgadmin (assuming pgadmin is the access point)

Related