#Django postgres

1 messages · Page 1 of 1 (latest)

snow harness
#

Hello everyone, maybe someone encountered such a problem in Django, I create a database using a makefile, specifically docker-compose, and the database itself is created, but I cannot connect

create-db:
docker-compose up -d db
docker-compose exec db sh -c 'psql --username=postgres --dbname=postgres -c "CREATE DATABASE $(PG_DB);"'
docker-compose exec db sh
-c 'psql --username=postgres --dbname=postgres -c "CREATE USER $(PG_USER) WITH PASSWORD '"'$(PG_PASSWORD)'"' SUPERUSER;"'
docker-compose exec db sh -c 'psql --username=postgres --dbname=postgres -c "GRANT ALL PRIVILEGES ON DATABASE $(PG_DB) to $(PG_USER);"'
docker-compose rm -s -f db

If I didn't write something correctly, I'm sorry, I'm a beginner in this business

deft topaz
#

share your docker compose yaml file please

snow harness
#
version: '3.8'

services:
  db:
    image: postgres

   ports:
      - ${PG_PORT}
    volumes:
      - pgdbdata:/var/lib/postgresql/data/
    env_file:
      - .env

  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: ["runserver", "0.0.0.0:8000" ]
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    env_file:
      - .env
    depends_on:
      - db

volumes:
  pgdbdata:
'''
deft topaz
#

#readme-1st for formatting

#

also what you're doing seems unecessary? postgres official docker image creates a database for you (POSTGRES_DB) and provides the POSTGRES_USER with superuser privileges

snow harness
#

This was created so that each user could deploy this project on their own, leaving us with 3 people working on it and this connection problem

deft topaz
#

in other words, you don't need this create-db you can just:

- db:
   image: postgres
   environment:
        POSTGRES_USER: ${PG_USER}
        POSTGRES_PASSWORD: ${PG_PASSWORD}
        POSTGRES_DATABASE: ${PG_DB}

Unless you explicitely plan on separating the postgres db super user from the PG_DB superuser.

snow harness
#

Yes, but it most likely does not affect the connection to the database

deft topaz
#

in terms of what you posted, the first mistake I see which is causing connection problems is docker-compose up -d db followed by docker-compose exec db sh -c 'psql...

docker-compose will not wait for the db to run before running the second command, hence it's daemonizing the database then instantly trying to connect to it.

snow harness
#

Yes, it certainly is , but how fix this problem

deft topaz
#

there are numerous "wait-for-it" or equivalent solutions that you can use OR you can roll your own, something along the lines of:

docker-compose up -d db
+./wait_for_it.sh docker-compose exec db pg_isready
docker-compose exec db sh -c 'psql --username=postgres --dbname=postgres -c "CREATE DATABASE $(PG_DB);"'

This uses pg_isready which is available in your postgres image to check whether the database is up and running. wait-for-it.sh will not continue before pg_isready succeeds (return 0 as error code, which bubbles up to the host)

snow harness
#

I will do this and mb write if not work)

deft topaz
#

you can also extend the postgres image and add the logic there, but I don't think you should do that now.