Cannot connect to dockerized Postgres through dockerized pgAdmin - postgresql

I have a docker-compose file:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: ayyy
POSTGRES_USER: letsgo
POSTGRES_PASSWORD: pwpwpwpw22
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: user#example.com
PGADMIN_DEFAULT_PASSWORD: pwpwpwpw1
ports:
- "5433:80"
(I changed the environment variables to not spoof my credentials)
However I am unable to connect to my Postgres server through pgAdmin. pgAdmin is using reverse proxy from port 5433 to my subdomain pgadmin.domain.com. I am also reverse proxying HTTP from pgAdmin's container into HTTPS. (I do not know if that could be an issue)
This is the error I get:
Unable to connect to server:
connection to server at "db" (192.168.32.3), port 5432 failed: timeout expired
If I use localhost as a hostname, I get this:
Unable to connect to server:
connection to server at "localhost" (127.0.0.1), port 5432 failed:
Connection refused Is the server running on that host and accepting
TCP/IP connections? connection to server at "localhost" (::1), port
5432 failed: Address not available Is the server running on that host
and accepting TCP/IP connections?
UPDATE: I am not even able to ping the containers between each other. The service name is correctly resolved to IP, but I get no response.

I was even unable to open ports on my server, so I reinstalled it. Everything works as it should since then.

Related

When running psql in a Docker container, how to do I reference my Postgres host in another Docker container?

I have the following two containers in my docker-compose.yml file
postgres:
image: postgres:10.5
ports:
- 5105:5432
...
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
env_file: .env
command: /usr/local/bin/gunicorn directory.wsgi:application --reload -w 1 -b :8000
volumes:
- ./web/:/app
depends_on:
- postgres
When I'm logged in to my "web" container (an Ubuntu 18 container), I'd like to be able to login to the PostGres container. How do I do this? I tried this
root#0868cef9c65c:/my-app# PGPORT=5432 PGPASSWORD=password psql -h localhost -Uchicommons directory_data
psql: 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?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
but this doesn't seem to be working.
In a Docker container, localhost refers to the container itself.
By default, Docker compose creates a docker bridge network and connects each container to it. From a container on the bridge network, you can reach other containers using their service names. So to reach the database container, you'd use postgres as the host name, like this
PGPORT=5432 PGPASSWORD=password psql -h postgres -Uchicommons directory_data
On the bridge network, you use the native ports. So it's port 5432 for Postgres. If you only need to access a container from other containers on the bridge network, you don't need to map the port to a host port. Mapping to a host port is only needed if you need to access the container from the host computer.

Adminer & Migrate Can't Connect to Postgresql Docker Container

I have three docker containers (postgresql, adminer, and go/migrate) and I've exposed both adminer and postgres ports to the host. I can access adminer in my browser, and postico can also connect to the DB. When I try to connect to the db from within adminer, it throws this error:
SQLSTATE[08006] [7] 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? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
The migrate container throws this error too:
error: dial tcp: 127.0.0.1:5432: connect: connection refused
So, clearly there is an issue with how the containers are able to communicate with each other. Do I need to create a docker network?
version: '3.1'
services:
db:
image: postgres
environment:
POSTGRES_DB: mydbname
POSTGRES_USER: mydbuser
POSTGRES_PASSWORD: mydbpwd
ports:
- "5432:5432"
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mydbpwd#localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
links:
- db
adminer:
image: adminer
restart: always
ports:
- "8081:8080"
depends_on:
- db
You do not need to create linking, docker-compose create default network. also service to service communication should use service name, localhost mean this container(migrate) not DB container.
change localhost to db
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mybpwd#db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
as you DB service name is db so you connect with db container using it name.

docker: get container's external IP

Running several docker containers including postgres database remotely.
$docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------
fiware-cygnus /cygnus-entrypoint.sh Up (healthy) 0.0.0.0:5050->5050/tcp, 0.0.0.0:5080->5080/tcp
fiware-elasticsearch /docker-entrypoint.sh elas ... Up 9200/tcp, 9300/tcp
fiware-grafana /run.sh Up 0.0.0.0:53153->3000/tcp
fiware-iotagent pm2-runtime bin/lwm2mAgent ... Up (healthy) 0.0.0.0:4041->4041/tcp, 5684/tcp, 0.0.0.0:5684->5684/udp
fiware-memcached docker-entrypoint.sh memca ... Up 11211/tcp
fiware-mongo docker-entrypoint.sh --bin ... Up 0.0.0.0:27017->27017/tcp
fiware-nginx nginx -g daemon off; Up 0.0.0.0:53152->53152/tcp, 80/tcp
fiware-orion /usr/bin/contextBroker -fg ... Up (healthy) 0.0.0.0:1026->1026/tcp
fiware-postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
fiware-wirecloud /docker-entrypoint.sh Up (healthy) 8000/tcp
I want to get the external IP address of the fiware-postgres container so I can connect via pgAdmin, instead of managing db via postgres client. It appears the IPAddress of fiware-postgres is only accessible internally.
$docker inspect fiware-postgres | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.3",
pgAdmin error:
Unable to connect to server:
could not connect to server: Operation timed out
Is the server running on host "172.19.0.3" and accepting
TCP/IP connections on port 5432?
Is there a way to get it's external IP, or at least some way of connecting via pgAdmin (remember docker container run on a remote, accessed via ssh).
EDIT
The remote host is accessible via ssh root#193.136.x.x:2222 so the postgres port 5432 cannot be reached.
pgAdmin settings(Connection Tab):
Host: 193.136.xx.xx
Port: 5432
Maintenance database: postgres
Username: postgres
Password: password
pgAdmin error:
Unable to connect to server:
could not connect to server: Operation timed out
Is the server running on host "193.136.x.x" and accepting
TCP/IP connections on port 5432?
postgres service definition (in docker-compose):
postgres:
restart: always
image: postgres:10
hostname: postgres
container_name: fiware-postgres
expose:
- "5432"
ports:
- "5432:5432"
networks:
- default
environment:
- "POSTGRES_PASSWORD=password"
- "POSTGRES_USER=postgres"
- "POSTGRES_DB=postgres"
volumes:
- ./postgres-data:/var/lib/postgresql/data
build:
context: .
shm_size: '2gb'
Assuming that you have exposed the ports of your container to the underlying host, then the application is accessible via the IP address of the underlying host.
The 172.19.0.3 IP address is an IP address that exists inside the Docker network on the host: it's not available externally. If your remote host has IP 1.2.3.4 and your application port has been exposed, e.g. in your compose file you have something like:
ports:
- "5432:5432"
Then your application should be accessible via 1.2.3.4:5432.
EDIT:
If you are connecting from your local machine to the remote server using pgAdmin, then there should be no need to ssh: you should find that the service is still available at 1.2.3.4:5432. However, if you have some form of firewall in the way (e.g. you're sshing in to an AWS server), then access to 5432 on the remote host may well be blocked. In that case, consider using port forwarding to route connections on your local server to the remote server, via the ssh connection.
ssh -L 5432:193.136.x.x:5432 193.136.x.x:2222
Now connect using pgAdmin to localhost:5432 or 127.0.0.1:5432.
Here's the solution that works for me:
ssh -p 2222 root#193.136.xx.xx -L 5432:localhost:5432
postgres server finally accessible via pgAdmin

Cannot connect to postgreSQL docker container via postico

I'm trying to use Postico to connect to a docker postgreSQL container on my local machine.
I've tried connecting to 0.0.0.0, localhost, and 127.0.0.1. Each give me the following error:
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
0.0.0.0 gives me a similar, but smaller error:
could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose file:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.23
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: prisma
migrations: true
postgres:
image: postgres:10.5
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Solution found thanks to Egor! I forgot to specify ports: - "5432:5432" inside my docker-compose file. Rookie mistake ;)
I also had issues using Postico to connect to my Postgres DB in a docker container.
Ultimately, my issue was that I had a local Postgres DB running.
As soon as I disconnected my local Postgres DB, I was able to use Postico to connect to my docker DB. With the host set to localhost, I used the POSTGRES_USER, POSTGRES_PASSWORD, and host port as defined in my docker-compose.yml file.
If postgres version doesn't matter, try to change Postgres image to this one, it works for me
And also make sure that you add ports in docker-compose.yml
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
ports:
- "5432: 5432"
volumes:
- postgres:/var/lib/postgresql/data
P.s. just updated answer for readability

Connect to docker postgres socket from host via docker volumes

I'm using the following docker-compose.yml file:
version: "3.5"
services:
db:
image: postgres:latest
volumes:
- ./tmp/postgresql/:/var/run/postgresql/:rw
ports:
- 5432:5432
environment:
POSTGRES_DB: dev
POSTGRES_USER: username
POSTGRES_PASSWORD: pw
I can connect to the postgres instance from my host as I have 5432 port forwarded to the host. I wanted to try connecting via the socket, but I am hitting issues. I'm not sure if this is possible?
From my host I can use the following:
psql --host=/Users/jalbert/Projects/postgres-sockets/tmp/postgresql -U username -d dev`
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/Users/jalbert/Projects/postgres-sockets/tmp/postgresql/.s.PGSQL.5432"?
Although, I do see the socket file present there. If I go within the docker container I can connect via the socket no problem using a similar command.
Am I missing something to allow the host to use the socket connection? Unfortunately, I am not too familiar with the socket technologies.