1

I want to connect to Postgres (running in a container) using psql

What I've done:

docker pull postgres
docker run -p5432:5432 --name mydb -e POSTGRES_PASSWORD=pass123 -d postgres

Trying to connect via PSQL command psql -h localhost -U postgres

But I keep getting psql: FATAL: role "docker" does not exist (I tried different variations of the command like with -d postgres or with -W , even tried with PgAdmin ...still same error)

However, when I jump into the container docker exec -it mydb bash then psql -U postgres I can see the following:

postgres=# \du
 List of roles
 Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
 postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \l
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
 | | | | | postgres=CTc/postgres
(3 rows)
asked Mar 14, 2019 at 7:44
0

1 Answer 1

1

Found the issue:

I had PSQL installed locally (via brew) which was showing the error message.

Uninstalled it. Then used PgAdmin to connect to the container. And it's working fine now.

answered Mar 14, 2019 at 10:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.