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
Related
So I am running the following command:
docker run --name psql-instance -d -p 5432:5432 -e POSTGRES_DB=mydb -e POSTGRES_USER=root -e POSTGRES_PASSWORD=pass postgres
This creates the container.
However when I run:
docker exec -it psql-instance psql -U root
I get the following error:
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "root" does not exist
I've seen many similar questions and they all say its docker-compose causing the error for them and that docker run works fine but for me, docker run doesn't work.
How can I fix this?
You need to tell psql to connect to the mydb database like this
docker exec -it psql-instance psql -U root mydb
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
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
I run docker container with following command
docker run --name postgres14 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=pass -d postgres:14-alpine
And I want to connect to my Postgres database and get error
FATAL: role "root" does not exist
My connection to db:
How to connect to my db?
Ok , this issue occurs only in Mac m1. Solution is run port 5433:5432
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.