Error: P1001: Can't reach database server at `localhost`:`5432` - postgresql

I'm having a problem when running the npx prisma migrate dev command. Docker desktop tells me that the database is running correctly on port 5432 but I still can't see the problem.
I tried to put connect_timeout=300 to the connection string, tried many versions of postgres and docker, but I can't get it to work.
I leave you the link of the repo and photos so you can see the detail of the code.
I would greatly appreciate your help, since I have been lost for a long time with this.
Repo: https://github.com/gabrielmcreynolds/prisma-vs-typeorm/tree/master/prisma-project
Docker-compose.yml
version: "3.1"
services:
postgres:
image: postgres
container_name: postgresprisma
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=santino2002
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Error:
Error: P1001: Can't reach database server at localhost:5432
Please make sure your database server is running at localhost:5432.
Docker ps show this:

Looks like the application and the database are running on two separate containers. So, in this case, connecting to localhost:5432 from the application container will try to connect to 5432 port within that container and not in the docker host's localhost.
To connect to database from the application container, use postgres:5432 (If they are on the same network) or <dockerhost>:5432.

Your docker ps output is showing that your postgres container has no ports connected to your local network.
It should look something similiar to this on ports column.
0.0.0.0:5432->5432/tcp, :::5432->5432/tcp
But yours is just 5432/tcp
You need to open ports for your postgres container.
Your docker-compose.yml file you posted in the question is correct. Probably you started postgres container with no ports first, then changed your docker-compose.yml file to have ports. So you just need to restart it now.
Use docker compose down && docker compose up --build -d to do that.

Related

Can't connect to the Postgres Docker container using SqlAlchemy

I have a Postgres Docker Container running locally, and the docker compose code for it looks like this
version: '3.9'
services:
db:
image: "postgres"
container_name: db
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=dbname
The database is started using the docker compose run db command
I then find the IP address of the container once it's running, which is "192.168.240.2"
When I try to connect to the database with SqlAlchemy like the following in a python program (this is on the same computer but outside of the container)
import sqlalchemy
engine = sqlalchemy.create_engine('postgresql://postgres:password#192.168.240.2:5432/dbname')
engine.connect()
It shows me this error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "192.168.240.2", port 5432 failed: Operation timed out
Is the server running on that host and accepting TCP/IP connections?
Anyone knows what the problem is here? thanks!
Tried searching for the error message, but changing the input to the create_engine() function according to other posts various ways still result in the same problem. However, I still imagine something's off with the string?

How to connect to a Postgres database running in local Docker container through locally-run psql command?

I'm running a docker container with the vanilla Postgres image on my local machine. I'd like to connect to the database from my local machine (i.e., not from "within the container". However, on trying to connect, I get an error.
Here's my docker-compose.yml file:
version: "3.8"
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mypassword
Here's how I start up:
docker-compose run db
Here's how I connect:
psql -h localhost -p 5432 -U postgres
This produces the error:
could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
If I spin up the database without Docker Compose, the same connection command works as expected:
docker run --name mypg -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
I could just go with the flow and use the command above. But this seems to be pointing to a flaw in how I think about Docker/Compose. For example, maybe Docker Compose's internal DNS resolver makes this approach fail.
Any ideas?
Version info:
psql --version
psql (PostgreSQL) 13.3
I have read through several SO posts, including these, but they don't address or fix the problem I'm seeing:
docker-compose: accessing postgres' shell (psql)
Can't connect to postgres when using docker-compose
Try docker-compose up db instead of run. Using run will run a one-off command against your container, whereas up will turn on the container and leave it running, so another application should be able to access it.
https://docs.docker.com/compose/faq/#whats-the-difference-between-up-run-and-start

What am I doing wrong in docker-compose for .netcore and postgres?

I am banging my head for a while on this issue and can't find what the issue might be. Running Docker Desktop on Windows 10. I have one dotnetcore 3.1 api that connects to postgres. Both of these are being run in containers.
Everything seems to work except connection to the database. Since I looked at my docker-compose.yml milion times, I can't come up with any other idea.
Here is my connection string:
"Server=postgres;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"
Here is docker-compose.yml file:
version: '3'
services:
identityserver:
depends_on:
- "postgres"
container_name: identityserver
build:
context: ./my_project/
dockerfile: Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT='Development'
ports:
- "5000:80"
postgres:
image: "postgres"
container_name: "postgres"
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=12345678
- POSTGRES_DB=IdentityManager
expose:
- "5432"
Everything builds up, but connection to database fails:
Unhandled exception. Npgsql.NpgsqlException (0x80004005): Exception while connecting identityserver
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::1]:5432
The weirdest thing is that when I run postgres alone with this same configuration on docker-compose.yml, and run the application outside of container with slightly different connection string:
"Server=127.0.0.1;Port=5432;Database=IdentityManager;User Id=postgres;Password=12345678;"
I am able to connect to database.
I tried cleaning everything docker system prune -a, tried restarting Docker, restarting PC, but to no awail. Can anyone try to help?
Finally, I was able to resolve my own problem and it wasn't in the docker-compose.yml file at all. Somewhere in the application code, connection string was changed to look for localhost as a host instead of postgres.
After changing it back to postgres, everything was fine.
try to
links:
- postgres
Maybe it will help

how to interpret db volume entries in a docker compose file

I'm in the process of setting up my first postgresql docker container, as part of a distributed application using docker compose, and am somewhat confused on the syntax of the compose file. Still pretty new to docker so forgive me if this is straight out of docker 101.
Here's what it looks like for my postgres container in docker-compose.yml:
version: '2'
services:
database:
image: postgres
container_name: database-container-name
environment:
- POSTGRES_PASSWORD=some_password_here
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- PGDATA=/var/lib/postgresql/data/db-files/
ports:
- 5433:5432
volumes:
- ./.db/data:/var/lib/postgresql/data:delegated
- ./.db/init:/docker-entrypoint-initdb.d
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder? I've looked at some documentation but it's not sinking in.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder?
In the volumes section of your database service, you're creating a couple of bind mounts between your host and your container. The syntax of each volume entry (in this situation) is HOST_PATH:CONTAINER_PATH:OPTIONS. So when you see:
- ./.db/data:/var/lib/postgresql/data:delegated
You are mapping the local (to your docker-compose.yml) .db/data directory onto /var/lib/postgresql/data in the container. In other words, changes made in one directory will be visible in the other.
The delegated option is specific to MacOS; the docs say:
delegated: The container runtime’s view of the mount is authoritative. There may be delays before updates made in a container are visible on the host.
The volume section of your service corresponds to the -v option to docker run; you may find more interesting in the docker run documentation in addition to the bind-mount docs I linked above.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The ports section is for publishing ports on your host. The syntax is HOST_PORT:CONTAINER_PORT. So when you see:
- 5433:5432
This is mapping host port 5433 to container port 5432. In other words, you can connect to your postgres database by connecting to port 5433 on your host. This is probably in place to avoid a conflict if you already had a postgres instance running on your host, which would already be bound to port 5432.

How to connect to OpenMapTiles Docker Postgres DB

I am currently playing around with openmaptiles (https://github.com/openmaptiles/openmaptiles) and I'd like to figure out how to import my own data into the resulting mbtiles. But first I'd like to look at how the postgres database it is using is structured.
I just can't figure out how I can connect to the postgres database using my GUI tool I have running locally.
I start postgres using the command provided on the help page:
docker-compose up -d postgres. Is it just not visible to outside of the docker container (I am also very new to docker)?
And is there a way to make it visible to my local system?
docker-compose up -d postgres refers to this part of the docker-compose.yaml file:
services:
postgres:
image: "openmaptiles/postgis:2.9"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- postgres_conn
ports:
- "5432"
env_file: .env
...
As you can see in the ports: section, there is no container - host port mapping here. To access this postgres database from your host try using "5432:5432". (notice that if you're already using this port on the host, you will have to pick an available one).
For more information on the docker-compose file reference and ports, check the docs.