Home > Software engineering >  Postgres db created in docker not shown in psql
Postgres db created in docker not shown in psql

Time:06-22

I entered a docker container (running a Postgres image) via bash and created a new database called test:

enter image description here

then I connected to it via command line:

psql -h localhost -p 5432  -U postgres

I inserted the password and got in, but when I look at the databases:

postgres=# \l

I only get 3 databases:

enter image description here

I'm using Windows 10, Docker version 20.10.16, psql (PostgreSQL) 14.4.

EDIT

Command for starting the container:

 docker run --name postgres-0 -e POSTGRES_PASSWORD=password -p 5432:5432 postgres:latest

Inside docker (test db present):

enter image description here

Outside docker connection (no test db):

enter image description here

CodePudding user response:

You're most likely connecting to two different instances. To access the instance inside the container you can use docker exec, e.g.

$ docker exec -u postgres postgres-0 psql -c "\l"

.. or add -it if you intend to keep working with psql in the same session

$ docker exec -itu postgres postgres-0 psql

EDIT (see comments): assuming postgres-0 is the container's name.

$ docker exec -u postgres postgres-0 psql -c "CREATE DATABASE test;"

$ docker exec -itu postgres postgres-0 psql -d test

CodePudding user response:

Here I've asked a better question and got the right answer and solved my problem...

Feel free to close this

  • Related