0

Better question and answer that solved my problem here.

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

IDK
  • 359
  • 1
  • 16
  • have you tried this? `docker exec -itu postgres yourcontainer psql` – Jim Jones Jun 21 '22 at 15:33
  • 2
    You are connecting to different Postgres instances, look at the output. One is English the other in Italian and the encodings/locales are different. Best bet is that in the second case you are connecting to database outside the Docker container. – Adrian Klaver Jun 21 '22 at 15:33
  • Why is `psql` generating PNG files; I'd normally expect it to produce plain-text output? How are you starting the container? – David Maze Jun 21 '22 at 15:42
  • 1
    It sounds like you’ve received an answer on your newer question about this? [Can't access Postgres instance inside docker](https://stackoverflow.com/questions/72705699/cant-access-postgres-instance-inside-docker) If so, it probably makes sense to close this original question. – Jeremy Caney Jun 21 '22 at 20:23
  • I want to but when I click on "delete" a thing pops up saying: "You cannot delete this question as others have invested time and effort into answering it. For more information, visit the help center." – IDK Jun 22 '22 at 05:49

1 Answers1

1

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
Jim Jones
  • 18,404
  • 3
  • 35
  • 44
  • isn't ```docker exec -it postgres-0 bash``` and then ```psql -U postgres``` and then creating my dbs the same things? – IDK Jun 21 '22 at 16:18
  • I've done exatcly as this guy suggests https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside – IDK Jun 21 '22 at 18:07
  • @IDK not necessarily. You can create and access the db without `bash`. See my last edit. – Jim Jones Jun 21 '22 at 19:06