Home > Enterprise >  PostgreSQL local data not showing in Docker Container
PostgreSQL local data not showing in Docker Container

Time:07-17

I just want some help here, I'm kinda stuck here in Docker and can't find a way out. First, I'm using Windows for a Django APP and Docker

I'm using PgAdmin4 with PostgreSQL 14 and created a new server for docker

The log for the Postgres Image:

2022-07-16 19:39:23.655 UTC [1] LOG:  starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-07-16 19:39:23.673 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-07-16 19:39:23.673 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-07-16 19:39:23.716 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-07-16 19:39:23.854 UTC [26] LOG:  database system was shut down at 2022-07-16 16:50:47 UTC
2022-07-16 19:39:23.952 UTC [1] LOG:  database system is ready to accept connections

PostgreSQL Database directory appears to contain a database; Skipping initialization

Log from my image: (you can see that doesn't have migrations)

0 static files copied to '/app/static', 9704 unmodified.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, controle, sessions
Running migrations:
  No migrations to apply.
Performing system checks...

System check identified no issues (0 silenced).
July 16, 2022 - 16:40:38
Django version 4.0.6, using settings 'setup.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

My docker-compose (updated):

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    networks:
      - django_net
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER = ${POSTGRES_USER}
      - POSTGRES_PASSWORD = ${POSTGRES_PASSWORD}
    ports:
      - "5432:5432"

  web:
    build: .
    command: >
            sh -c "python manage.py collectstatic --noinput &&
                   python manage.py migrate &&
                   python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db
    environment:
      - POSTGRES_NAME=${POSTGRES_NAME:-djangodb}
      - POSTGRES_USER=${POSTGRES_USER:-postgre}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgre}
    networks:
      - django_net


networks:
  django_net:
    driver: bridge

And my .env file (updated):

SECRET_KEY='django-insecure-1l2oh_bda$@s0w%d!@qyq8-09sn*8)6u-^wb(hx03==(vjk16h'
POSTGRES_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=mypass
POSTGRES_DB=mydb
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]

So, analyzing the logs from Postgres last line, he found my local DB (is that right ?) and didn't initialize, but my superuser is gone and so my data.

Is there something that I'm missing ? Maybe it's like that, and I don't know... Just to be sure, I printed some lines from PGAdmin and the APP Screen

DB:

My DB data

My APP:

Nothing

My settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': 'db',
        'PORT': 5432,
    }
}

CodePudding user response:

If I correct understood your question, you can't connect to created database.

If you want to connect to your containerized docker database from outside, you should define ports parameter in your db service in docker-compose file.

 services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    networks:
      - django_net
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    ports:
      - "5432:5432"

I hope I correct understood your question about you can't connect to new database and I hope my answer will help you.

CodePudding user response:

In this setup, I see two things:

  1. You've configured DATABASE_URL to point to host.docker.internal, so your container is calling out of Docker space, to whatever's listening on port 5432.

  2. In your Compose file, the db container does not have ports:, so you're not connecting to the database your Compose setup starts.

This implies to me that you're running another copy of the PostgreSQL server on your host, and your application is actually connecting to that. (Maybe you're on a MacOS host, and you installed it via Homebrew?)

You don't need to do any special setup to connect between containers; just use the Compose service name db as a host name. You in particular do not need the special host.docker.internal name here. (You can also delete the networks: from the file, so long as you delete all of them; Compose creates a network named default for you and attaches containers to it automatically.) I might configure this in the Compose file, overriding the .env file

version: '3.8'
services:
  db: { ... }
  web:
    environment:
      - DATABASE_URL=postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@db/$(POSTGRES_DB)

CodePudding user response:

I hope my answer to help you solve the problem. Please change the config as follow: version: "3.9"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    networks:
      - django_net
    environment:
      - POSTGRES_DB=${POSTGRES_DB:-djangodb}
      - POSTGRES_USER = ${POSTGRES_USER:-postgres}
      - POSTGRES_PASSWORD = ${POSTGRES_PASSWORD:-changeme}
    ports:
      - "5432:5432"

  web:
    build: .
    command: >
            sh -c "python manage.py collectstatic --noinput &&
                   python manage.py migrate &&
                   python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db
    environment:
      - POSTGRES_NAME=${POSTGRES_NAME:-djangodb}
      - POSTGRES_USER=${POSTGRES_USER:-postgre}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgre}
    networks:
      - django_net


networks:
  django_net:
    driver: bridge
  • Related