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
after creating db in docker with:
docker run -it \
-e POSTGRES_USER="root" \
-e POSTGRES_PASSWORD="root" \
-e POSTGRES_DB="ny_taxi" \
-v /Users/ruslanpilipyuk/Desktop/data-engineering-zoomcamp-main/week_1_basics_n_setup/2_docker_sql/ny_taxi_postgres_data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:13
But when I try to connect to it for example with sqlalchemy, I get this:
OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: FATAL: role "root" does not exist
(Background on this error at: http://sqlalche.me/e/14/e3q8)
Same error happens when using pgcli to connect with.
(base) ruslanpilipyuk#MacBook-Pro-Ruslan 2_docker_sql % pgcli -h localhost -p 5432 -u root -d ny_taxi
connection to server at "localhost" (::1), port 5432 failed: FATAL: role "root" does not exist
I have used also, standard superuser postgres, and it is not working either with same error:
connection to server at "localhost" (::1), port 5432 failed: FATAL: role "postgres" does not exist
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.
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.
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.