Search code examples
pythonpython-3.xdjangodockerpsql

How to browse django psql backend from command line?


I'm developing a website with Django 3 (in a docker container) using postgres sql as the backend; i.e. in the project settings file I have:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432
    }
}

I've populated the backend database and can browse the data using the admin. However, Id like to connect to the database via the command line so I can more easily test queries. I have tried connecting to the database the normal way from the command line:

sudo -u postgres psql
postgres=# \c postgres

The problem is that there is no data found:

postgres=# \dt
Did not find any relations.

Since I'm new to docker I thought to try connecting other ways as well; specifically, based on another post I tried:

sudo docker run -d -p 5432 -t postgres/postgresql /bin/su postgres -c '/usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf'

This throws an error:

pull access denied for psql/postgresql, repository does not exist or may require 'docker login'

Again, I'd like to connect to the database via the command line so I can more easily test queries. Perhaps Im on the right track but assistance would be appreciated.


Solution

  • UPDATE, my docker-compose.yml file had the following service:

    db:
            image: postgres:11
            volumes:
                    - postgres_data:/var/lib/postgresql/data/
    

    I am able to connect to the Django backend using the following command:

    sudo docker-compose exec db psql -U postgres
    postgres=# \c postgres
    

    As suggested by @FlipperPA I will start useing a different username and database name