2

I start learning docker with Django+PostgreSQL, I did all steps from there https://docs.docker.com/samples/django/

How can I open created database on terminal or pgAdmin to see tables and change them?

I use: Ubuntu 22.04 / Docker Engine 20.10.17 / Docker Compose version v2.6.1 / psycopg2-binary==2.9.3 / Django==4.0.6 / psql (13.7 (Ubuntu 13.7-1.pgdg22.04+1))

my docker-compose.yml:

version: "3.9"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db

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,
}
}

some info if needed, postgres file created are owned by root:

postgres file 1/2

postgres file 2/2

docker-compose commands

Thanks for answers!

almanac
  • 23
  • 4

1 Answers1

2

Have you tried this?

docker exec -it {your postgres container name} psql -U {your db username} {your db name}

or simply

docker compose exec {service name} manage.py dbshell
NamsanCode
  • 226
  • 1
  • 4
  • Yep, but nothing: https://imgur.com/a/zRi9Bd0 – almanac Jul 06 '22 at 17:53
  • when you run those commands what's the exact results? any error message? – NamsanCode Jul 06 '22 at 21:48
  • yes, i did and add the screen named by 'docker-compose commands' 1 error mes: Error: No such container: postgres 2 error mes: OCI runtime exec failed: exec failed: unable to start container process: exec: "manage.py": executable file not found in $PATH: unknown – almanac Jul 06 '22 at 22:37
  • @almanac Let's read throught the error logs. `Error: No such container: db` Based on the screencapture you added, your postgres container name is `django1-db-1`, So the command should be like this: ``` docker exec -it django1-db-1 psql -U postgres postgres ``` (btw sharing logs in text format would be much better than images) About this(`manage.py executable file not found: $PATH unknown`), you can refer to https://stackoverflow.com/questions/59209889/docker-compose-executable-file-not-found-in-path-unknown . – NamsanCode Jul 07 '22 at 00:44
  • [1] First command helped, but when I execute the command, I took this text 'psql (14.4 (Debian 14.4-1.pgdg110+1))', I still don't know why Debian. Thanks a lot man! – almanac Jul 07 '22 at 12:35
  • is that the entire result you got? – NamsanCode Jul 08 '22 at 02:32