baxx
baxx

Reputation: 4705

Unable to connect to postgres docker image

Following this video: https://www.youtube.com/watch?v=G3gnMSyX-XM I've entered the following:

➜  Downloads docker run -p 5432:5432 -d \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=stripe-example \
-v pgdata:/var/lib/postgresql/data \
postgres
5aaa30effe3282bd06c52b6c71f8343d24df041e9af11a38eb7a1e49453e3fa6
➜  Downloads psql stripe-example -h localhost -U postgres
psql: error: FATAL:  role "postgres" does not exist

but am not sure why the role postgres doesn't exist, as it seems I've created that with -e POSTGRES_USER=postgres.

I'm expecting the following (screenshot from this timestamp in the video):

enter image description here

Some updates.

I looked at the docs and dash isn't allowed in database names. If your change the database name to stripe_example, it works. Remember to delete the pgdata volume first, before you try again.

➜  Downloads docker run -p 5432:5432 -d \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=stripe_example \
-v pgdata:/var/lib/postgresql/data \
postgres
da57c37c8306d5afdb54f8b3cf5e16aa67f97bce85ff4c8d3340e6f3288fb3cd
➜  Downloads psql stripe_example -h localhost -U postgres
psql: error: FATAL:  database "stripe_example" does not exist

Upvotes: 0

Views: 597

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25234

I looked at the docs and dash isn't allowed in database names. If your change the database name to stripe_example, it works. Remember to delete the pgdata volume first, before you try again.

https://www.postgresql.org/docs/7.0/syntax525.htm#:~:text=Names%20in%20SQL%20must%20begin,but%20they%20will%20be%20truncated.

These commands should let you get to a state where I am at and where it works

docker volume rm pgdata
docker run --name=mypostgres -p 5432:5432 -d -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=stripe_example -v pgdata:/var/lib/postgresql/data postgres
docker exec -it mypostgres psql stripe_example -U postgres

One thing I do differently is that I run psql from inside the container, because I haven't got Postgres installed on my host. But that shouldn't make a difference.

Upvotes: 1

Related Questions