How to access Postgres database within docker-compose

Dominic M. picture Dominic M. · Oct 25, 2017 · Viewed 11.7k times · Source

I'm pretty new to Postgres and Docker, but I am building a webapp with Django that runs inside a docker with a postgresql database. I am trying to view my Postgres database via the command prompt or pgweb.

Unfortunately, I can't seem to find the database at all, I tried using the postgres psql terminal commands, but my database wasn't listed inside. My question is, how is the postgresql database stored within a docker-compose container. Do I need to go into the container manually and then view the database? My ultimate goal is to be able to see a list of all my tables and data within the postgresql database.

Any documents explaining how the postgresql database interacts with a docker-compose would be appreciated.

Thanks!

I just recently added Pgweb, but that doesn't seem to be working

Local.YML

version: '2'

volumes:
  postgres_data_local: {}
  postgres_backup_local: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    depends_on:
      - postgres
    volumes:
      - .:/app
    environment:
      - POSTGRES_USER=vicki
      - USE_DOCKER=yes
    ports:
      - "8000:8000"
    command: /start.sh

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    volumes:
      - postgres_data_local:/var/lib/postgresql/data
      - postgres_backup_local:/backups
    environment:
      - POSTGRES_USER=vicki

  pgweb:
    container_name: pgweb  
    restart: always  
    image: sosedoff/pgweb
    ports:
      - "8081:8081"
    links:
      - postgres:postgres  
    environment:
      - DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
    depends_on:
      - postgres 

This is the Postgres Terminal psql I am talking about, When I open my terminal and run:

postgres@dom-Inspiron-7559:~$ psql
psql (9.3.19)
Type "help" for help.

postgres=# \l
postgres=# 

This list of databases appears, but I don't believe vickibot is the same as vicki. (I had made two apps with django cookiecutter, one within docker and one not in docker. I believe the one I am using right now is "vicki" that is within a docker container, but again i'm not sure and don't know how to tell what database my models file is referencing. I am wondering if I need to somehow enter the docker container to view the database)

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test1     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 vickibot  | dominic  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

Answer

Matt picture Matt · Oct 26, 2017

To access postgres in the container, run the cli in the scope of the container

docker-compose run postgres psql

You can also connect to the containers published port, which hasn't been published in your existing config.

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    volumes:
      - postgres_data_local:/var/lib/postgresql/data
      - postgres_backup_local:/backups
    ports:
      - '5433:5432'
    environment:
      - POSTGRES_USER=vicki

Then connect to your published port

psql -h localhost -p 5433