No response from postgres server - postgresql

I'm trying to set up a small postgresql server on my raspberry pi. So far, I've:
Installed postgres onto my pi
Set a static IP adress for my pi
edited /etc/postgresql/13/main/postgresql.conf so that listen_addresses = '*'
edited /etc/postgresql/13/main/pg_hba.conf to include
host all all 0.0.0.0/0 md5
Running pg_isready on my pi gives me
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
However, running the following results in no response
root#pibox:~# pg_isready -d <db_name> -h<static_ip> -p 5432 -U postgres
<pi's IP>:5432 - no response
Additionally, I'm not able to connect from my laptop
psql --host=<static IP> --port=5432 --dbname <db name> --username=postgres
psql: error: connection to server at "<the pi's IP>", port 5432 failed: Connection refused
What are some of the next steps I can take so that I can connect to my postgres server from my laptop and execute queries against the databases?
EDIT:
My laptop is running macOS 12.4. I'm connecting to my pi via ssh over my home network. The pi is running a copy of DietPi. Here is some more relevant info
root#pibox:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Running the following commands yeilds these outputs
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
root#pibox:~# pg_isready -d postgres -h <Pi IP> -p 5432 -U postgres
<Pi IP>:5432 - no response
root#pibox:~# pg_isready -d postgres -h 127.0.0.1 -p 5432 -U postgres
127.0.0.1:5432 - no response
root#pibox:~# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log

Related

Unable to connect to postgres sql on Kubernetes minikube from Client tool

I have successfully deployed postgres on Kubernetes.
postgres NodePort 10.96.66.202 <none> 5432:30030/TCP
I am able to connect to postgres using localhost and 5432 with the following command:
kubectl exec -it postgres-75b8fd84f-gkj6k -- psql -h localhost -U appuser --password -p 5432 appdb
But when I tried to access the psql using another client tool using the node port and minikube IP I am getting the below error:
host=$(minikube ip)
192.168.49.2
port=$(kubectl get service postgres -o jsonpath='{.spec.ports[0].nodePort}')
30030
$ psql -h 10.96.66.202 -U appuser --password -p 30030 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "10.96.66.202" and accepting
TCP/IP connections on port 30030?
Edit:
I tried the minikube IP as well:
$ psql -h 192.168.49.2 -U appuser --password -p 30030 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "192.168.49.2" and accepting
TCP/IP connections on port 30030?
I also tried this combination as well:
$ psql -h 10.96.66.202 -U appuser --password -p 5432 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "10.96.66.202" and accepting
TCP/IP connections on port 5432?
The IP 192.168.49.2 is for the node, and 10.96.66.202 is for the service.
And the nodePort 30030 is for the node, and the targetPort 5432 is for the service.
So you can use either
psql -h 192.168.49.2 -U appuser --password -p 30030 appdb
or
psql -h 10.96.66.202 -U appuser --password -p 5432 appdb

Cannot connect to database on Docker

I have created database on docker:
docker run --name database -e POSTGRES_PASSWORD=password -d -p 5436:5436 postgres
Created some database inside:
createdb database
Also created superuser. Now I am trying to connect from localhost:
psql -h localhost -p 5436 -U postgres
After that I get following error:
psql: The server closed the connection unexpectedly
This probably means that the server has terminated abnormally
before or while processing your inquiry.
Ports are connected:
In your case, you are trying to expose 5436 and map with your host port 5436, but that might need to use -p 5436:5432 because Postgres uses port 5432 as the default port.
docker run --name database -e POSTGRES_PASSWORD=password -d -p 5436:5432 postgres
There is some description from Container networking
-p 8080:80:Map TCP port 80 in the container to port 8080 on the Docker host.

Postgres in Docker port matching

I need to run several equals Postgres containers on different ports via docker-compose. My problem is that I do not understand how to connect from my terminal to container's psql.
First of all, I should say that my local Postgres is off, so 5432 is free. Also I have listen_addresses = '*' in my containers.
Here is list of my attempts (to make it easier, I've posted examples for one Postgres container):
Run without port specifying
docker-compose.yml:
version: "3"
services:
postgres:
build:
context: ../../
dockerfile: db-load-test/sharding-benchmark/Dockerfile
environment:
- POSTGRES_PASSWORD=postgres
check port:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
try to connect:
$ psql -p 5432 -U postgres
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
$ psql -h localhost -p 5432 -U postgres
psql: 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?
$ psql -h 0.0.0.0 -p 5432 -U postgres
psql: 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?
Specify port and expose it in .yaml (I've changed 5432 to 5433 to make it more expressive)
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp, 0.0.0.0:49162->5433/tcp,:::49162->5433/tcp
Similar attempts to connect was unsuccessful. Also it's unclear to my why Postgres still listen 5432:
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-08-02 12:49:01.397 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Use docker directly
docker run --rm -it -e POSTGRES_PASSWORD=postgres -p 5432:5432/tcp postgres:11
It works with psql -h localhost -p 5432 -U postgres, but if I use docker run with 5432:5433 it breaks, which is unclear to me.
In your docker-compose.yml file, you need to specify ports: for each of the database containers. In each of these the second number must be the normal port for the database (5432); the first number can be any number of your choosing that's not used by another container or host process.
version: '3'
services:
pg1:
image: postgres
# Use default PostgreSQL port 5432 on the host
ports: ['5432:5432']
pg2:
image: postgres
# First port must be different from pg1's above
# Second port must be exactly 5432
ports: ['5433:5432']
When you connect from the host, use a host name of localhost and the first published ports: number. (If you're using the older Docker Toolbox setup, use the docker-machine ip address of the Docker VM; if you're connecting from a different host, use the Docker host's DNS name or IP address.)
# connect to pg1
psql -h localhost -p 5432 -U postgres
# connect to pg2
psql -h localhost -p 5433 -U postgres
You do not need to expose: ports in the docker-compose.yml file; this doesn't really actually do anything. You shouldn't need to reconfigure the stock postgres image beyond its basic environment-variable settings. It doesn't make sense to connect to 0.0.0.0, a special address that means "everywhere" when you're setting up a network listener.
If you're connecting from a different container, none of this matters. Use the Compose service name of the other container (like pg2) and the standard port number (5432). ports: are completely ignored, and localhost means "this container". The two containers must be on the same Docker network; Compose will create a network named default and just using that without explicit networks: settings is fine.

Hasura container cannot connect to postgres DB?

I am running a postgres db running in Docker with:
docker run -d -p 5432:5432 --name db --env POSTGRES_PASSWORD=postgres --env POSTGRES_USER=postgres postgres
I'm trying to connect Hasura to it, with this command
docker run --name hasura -p 5002:8082 --env HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:postgres#localhost:5432 -e HASURA_GRAPHQL_ENABLE_CONSOLE=true hasura/graphql-engine:latest
but I get this response:
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"info","detail":{"kind":"server_configuration","info":{"live_query_options":{"batch_size":100,"refetch_delay":1},"transaction_isolation":"ISOLATION LEVEL READ COMMITTED","plan_cache_options":{"plan_cache_size":4000},"enabled_log_types":["http-log","websocket-log","startup","webhook-log"],"server_host":"HostAny","enable_allowlist":false,"log_level":"info","auth_hook_mode":null,"use_prepared_statements":true,"unauth_role":null,"stringify_numeric_types":false,"enabled_apis":["metadata","graphql","config","pgdump"],"enable_telemetry":true,"enable_console":true,"auth_hook":null,"jwt_secret":null,"cors_config":{"allowed_origins":"*","disabled":false,"ws_read_cookie":null},"console_assets_dir":null,"admin_secret_set":false,"port":8080}}}
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"info","detail":{"kind":"postgres_connection","info":{"retries":1,"database_url":"postgres://postgres:...#localhost:5432"}}}
{"type":"pg-client","timestamp":"2021-04-20T05:49:08.548+0000","level":"warn","detail":{"message":"postgres connection failed, retrying(0)."}}
{"type":"pg-client","timestamp":"2021-04-20T05:49:08.548+0000","level":"warn","detail":{"message":"postgres connection failed, retrying(1)."}}
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"error","detail":{"kind":"catalog_migrate","info":{"internal":"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}}}
{"internal":"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}
I am running Docker Desktop v3.3.1 on Windows 10.
Any help on this would be appreciated.
Thanks.
As anemyte mentioned, changing localhost to host.docker.internal works for me.

Docker container Postgres connection error

i have been trying out docker container for postgres. So far i have not been able to connect to the database inside the container.
My steps to recreate the problem below.
dockerfile start
FROM postgres
ENV POSTGRES_DB db
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY db_schema.sql /docker-entrypoint-initdb.d/
I built the docker file like so
$ docker build -t db_con .
And created a container
$ docker run -it -p 5432:5432 --name test_db db_con /bin/bash
View of running container as below
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82347f1114c4 db "docker-entrypoint..." 3 hours ago Up 2 sec 0.0.0.0:5432->5432/tcp test_db
I inspected the container for the address info..
$ docker inspect test_db
--extract start--
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
--extract end--
Now, i have tried within the container, but i have NOT been successful.
I have tried all the commands below with the error below.
root#82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
root#82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
root#82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
**response for all the above**
psql: 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?
I will be delighted if anyone can point me in the right direction. I've hit a wall here, any assistance is much appreciated.
it looks like you override default postgres cmd to /bin/bash.
Why do you put /bin/bash at the end of command?
docker run -it -p 5432:5432 --name test_db db_con /bin/bash
Try to execute
docker run -it -p 5432:5432 --name test_db db_con
Also, postgres will be available only when db dump was restored.
You need to add a new rule in your pg_hba.conf:
nano /etc/postgresql/9.3/main/pg_hba.conf
Add:
host all all [Docker Server IP]/16 md5
Next, you need to uncomment the follow line in the postgres.conf:
listen_addresses = '*' # what IP address(es) to listen on;
Now restart your postgres service, and try again.