I'm trying to set up a PostgreSQL database using the Docker image, but I'm hitting a snag. I created a container with the command: `docker run --name postgres-container -e POSTGRES_PASSWORD= -v pgdata:/var/lib/postgresql -p 5432:5432 -d postgres`. After that, I accessed the container with `docker exec -it postgres-container psql -U postgres` and created a database named `my_db`. However, when I attempt to connect to this database from my Node.js application running on my local machine, I get an error saying that the database doesn't exist. I checked with pgAdmin to see if the database was really there, but it's missing. I've heard about a workaround involving running a new container on a different port (5433) to access the PostgreSQL server on port 5432. Can anyone explain why I'm facing this issue, why switching ports might help, and how I can successfully connect to port 5432 from my localhost?
2 Answers
I encountered a similar issue and found that permissions inside the container impacted visibility from pgAdmin. Ensure that the user you're using in pgAdmin has access to `my_db`. If you're troubleshooting connections, using `docker logs postgres-container` can provide insights into any errors or issues happening at the database level.
You might want to check how you're connecting to the database from Node.js. Make sure the connection string specifies the correct host, port, database name, and user credentials. Since you're using Docker on Windows, the 'localhost' might not point to your Docker container properly. Try using `host.docker.internal` instead of `localhost`. This will route your request to the Docker daemon running on your host machine.
Using `host.docker.internal` sounds like a great idea! I had similar issues on Windows and switching the host helped a lot.

Thanks for the tip! I'll check the logs and review the user permissions.