0

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
2
  • If the pgdata volume already existed with a postgres database in it, then the startup script won't create a new user. If you think that might be the case, then you can remove the volume with the command docker volume rm pgdata. Commented Dec 5, 2021 at 21:45
  • @HansKilian thanks - after deleting that volume I instead get the error: psql: error: FATAL: database "stripe-example" does not exist, which I thought was created with -e POSTGRES_DB=stripe-example Commented Dec 5, 2021 at 21:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I've edited the post - I tried this and it didn't work though
Weird. It works when I do it.
I'm not sure if there's more information I can add to the post to make it clearer?
If you look at the logs of the container, does it somewhere in the beginning say CREATE DATABASE?
I've added the commands I run to my answer.

Your Answer

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.