2
votes

I have a docker compose file that looks like:

version: "3"
services:
    redis:
        image: 'redis:3.2.7'
        # command: redis-server --requirepass redispass

    postgres:
        image: postgres:9.6
        ports:
            - "5432:5432"
        environment:
            - POSTGRES_USER=airflow
            - POSTGRES_PASSWORD=airflow
            - POSTGRES_DB=airflow
#            - PGDATA=/var/lib/postgresql/data


    webserver:
        image: airflow:develop
        depends_on:
            - postgres

        ports:
            - "8080:8080"

        command:
            - webserver

After I run docker-compose up I see all the services started and seemingly working well. My webserver service connects to postgres with the following sqlalchemy connection string: postgresql+psycopg2://airflow:airflow@postgres/airflow

I'd like to be able to inspect the postgres db that's created from my host. I've tried several variants of psql but they all fail:

 8:49AM /Users/paymahn/solvvy/scheduler gitlab-ci ✚ ◼
 ❮❮❮ psql -h postgres -p 5432 -U airflow
psql: could not translate host name "postgres" to address: nodename nor servname provided, or not known
 8:50AM /Users/paymahn/solvvy/scheduler  ✘ 2 gitlab-ci ✚ ◼
 ❯❯❯ psql -h localhost -p 5432 -U airflow
psql: FATAL:  database "airflow" does not exist
 8:51AM /Users/paymahn/solvvy/scheduler  ✘ 2 gitlab-ci ✚ ◼
 ❯❯❯ psql -h localhost -p 5432 -U postgres
psql: FATAL:  role "postgres" does not exist

How can I connect to the db inside the postgres container from the host running the container?

EDIT: this is answered in a comment in the accepted solution but I think it's worth pointing out up here: I was already running postgres locally which was causing all sorts of confusion.

10:12AM /Users/paymahn/solvvy/scheduler gitlab-ci ✚ ◼
 ❯❯❯ ps aux | rg postgres
paymahn          11705   0.0  0.0  4485040    596   ??  Ss   13Feb18   0:00.17 postgres: bgworker: logical replication launcher
paymahn          11704   0.0  0.0  4339972    680   ??  Ss   13Feb18   0:13.88 postgres: stats collector process
paymahn          11703   0.0  0.0  4485040   1404   ??  Ss   13Feb18   0:02.72 postgres: autovacuum launcher process
paymahn          11702   0.0  0.0  4486064    116   ??  Ss   13Feb18   0:06.42 postgres: wal writer process
paymahn          11701   0.0  0.0  4476848    204   ??  Ss   13Feb18   0:02.62 postgres: writer process
paymahn          11700   0.0  0.0  4486064    228   ??  Ss   13Feb18   0:00.58 postgres: checkpointer process
paymahn          11698   0.0  0.0  4478128    592   ??  S    13Feb18   0:12.09 /usr/local/Cellar/postgresql/10.2/bin/postgres -D /usr/local/var/postgres
paymahn          92143   0.0  0.0  4262040     24 s011  S+   10:12AM   0:00.00 rg postgres

Killing my local instance of postgres and updating my hosts file as mentioned in the accepted answer cleared everything up.

1

1 Answers

1
votes

Just add 127.0.0.1 postgres to hosts file on your local PC and then use command psql -h postgres -p 5432 -U airflow