Can't run psql backup in user-data script in AWS EC2 - postgresql

I have private subnet for my EC2 instance, I use NAT Gateway for it to access internet. I created user-data script for my EC2 where I've created docker-compose.yml with postgres service defined. After defining this i have:
docker-compose up -d
# i use terraform for ${PGPASSWORD} template variable
export PGPASSWORD="${PGPASSWORD}"
psql -h localhost -U my_user -p 5432 -d my_db < ./mydump.sql
Inside EC2 I already have mydump.sql file.
The problem here it gives me error for psql like this:
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
I can't understand why. Although my postgres container is up and LOG: database system is ready to accept connections. Can someone help with this?
Maybe this problem relates to root under which user-data script is executed and session closes so it interrupts connection for psql. Or this relates to docker and port-forwarding. Can't understand the reason actually

Related

unable to launch psql command while running a postgres contenair

i try on my localhost recommended commands to learn playing with docker.
The exact command is :
docker run -it --rm postgres psql
The error message i get is :
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
In fact the file .s.PGSQL.5432 does no exist in the container, while it exists
on the host machine.
So, what is wrong in my reasoning/command ?
You should think of containers as, conceptually, separate machines. Separate from the host and separate from each other.
When you run psql without any parameters, like you do here, it'll look for a postgres database running on the local machine, on port 5432. But since psql is running in a container it looks for the database inside the container. And there isn't one. That's what the error message is trying to tell you.
To get it to work, you need to specify the -h parameter on the psql command to tell it where the database is located. To get the address of the host machine, you can add --add-host=host.docker.internal:host-gateway to the docker run command. It's customary to call the host host.docker.internal.
So you end up with the command
docker run -it --rm --add-host=host.docker.internal:host-gateway postgres psql -h host.docker.internal
which should then let you connect to the postgres database on the host machine.

PostgreSQL: Can't Connect to Socket on Host via Volume in Docker

This is seemingly the same as this issue, though I thought I'd provide a simple example:
docker run -it \
-v /pg_socket_on_host:/pg_socket_in_container \
-e PGPASSWORD=${PGPASSWORD} \
postgres \
psql -h /pg_socket_in_container -U postgres postgres
Where the path /pg_socket_on_host is a directory containing the file .s.PGSQL.5432. I've tried a few different versions of this, but I keep ending up with the same result:
psql: error: connection to server on socket "/pg_socket_in_container/.s.PGSQL.5432" failed: Connection refused
Is the server running locally and accepting connections on that socket?
Is there a reason that this is a problem with Docker?
Follow up:
I ensured that the permissions and the user (name and id, as well as group and id) for the host and container path/volume line up based on this post, but I still get the same error. I am able to connect to the socket on the host machine from the host machine. I am also able to connect to the host via host.docker.internal from the docker container. Any other ideas about debugging strategies?

how to connect pgadmin container to host system localhost postgres [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I pulled pgAdmin4 docker image into my linux debian machine and followed the process specified here to configure the container. I run docker run -p 8000:8000 --env-file ./pgadmin_docker_env.list -d dpage/pgadmin4. For clarity, the pgadmin_docker_env.list specified in the command contains the environmental variables:: PGADMIN_DEFAULT_EMAIL=my_email#example.com PGADMIN_DEFAULT_PASSWORD=my_password. With the container running in detached mode, I run localhost:8080 in my web browser to access pgAdmin 4 in server mode. However, I was unable to create a server connection to the localhost postgres database from inside the pgadmin. I got the following error after input of the connection parameters (shown in the screenshot attached below)
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?
UPDATE
I used host.docker.internal in place of localhost but I still got an error
Unable to connect to server: could not translate host name "host.docker.internal" to address: Name does not resolve
You can skip a step if you've already done it
Using psql, alter the authentication credential of default postgres user, postgres with the following commands
sudo -u postgres psql
ALTER USER postgres PASSWORD 'newPassword';
Optionally, you can also create a user for your current account as a superuser with CREATE ROLE user_name WITH LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION;
Modify /etc/postgresql/13/main/pg_hba.conf and add
host all all 0.0.0.0/0 md5 to the end of the file
Modify the pgadmin_docker_env.list file to include your choice port
PGADMIN_LISTEN_PORT=8000
Stop the previously running container pgadmin docker stop pgadmin and remove the containerdocker rm pgadmin. Then run docker run --env-file ./pgadmin_docker_env.list --network="host" --name pgadmin dpage/pgadmin4 to run the container in host network mode. See more on host network mode
Run localhost:8000 in your web browser and create a server connection using the same connection parameter as in the screenshot.
localhost in this scenario refers to the PgAdmin container, where there is not a Postgres instance running.
You want to connect to Postgres running on the host machine from the container (from what I can tell anyway?) so use host.docker.internal instead of localhost.

How to connect to postgres by psql (-h localhost -p 5432) when docker container starts

I have some docker-compose.yml file. In this file is defined db service (postgres:9.6):
db:
image: postgres:9.6
volumes:
- ./test/data/postgresql/:/var/lib/postgresql/data:delegated
- ./test/bootstrap/postgres:/docker-entrypoint-initdb.d:delegated
ports:
- 15432:5432
environment:
- POSTGRES_DB=test
- POSTGRES_USER=test
- PGDATA=/var/lib/postgresql/data/testdata
And some sh script located in ./test/bootstrap/postgres. In this script i try to connect to postgres by psql like that: psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h localhost -p 5432 (I know that -h localhost -p 5432 is not necessary but i need it).
When my container is up error occured:
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?
But after this i restart (after first start this script not executed) my db container then inside in container manually run sh script and it executed successfully. It can connect to localhost and port 5432 even if it is first start.
If i connect in script like that psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" there is no connection error.
Can someone explain me this strange behavior? Why i can't connect to localhost:5432 when container is starting but if connect manually from container it connect successfully?
Thanks!
My understanding is that during the initialization of the container the server is listening on Unix-domain socket.
LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
The scripts in the docker-entrypoint-init.d directory are executed while the server runs in this mode. You're trying to connect via TCP/IP by specifying -h localhost.
Try to remove that argument to connect to the server using Unix sockets.
try to add a sleep command in your script to give enougth time to your database to start correctly.
According to docs at hub.docker.com/_/postgres, to add a custom script to run when your container starts, create a shell script like the one below and create a custom Dockerfile where you'll copy the shell script into /docker-entrypoint-initdb.d/
Assuming you save the following script as initdatabase.sh in the same folder as your custom Dockerfile also defined below in example, when you build the image, you'll have created your custom image which runs the following sql statements defined below.
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER user;
CREATE DATABASE customdatabase;
GRANT ALL PRIVILEGES ON DATABASE bloodapp TO user;
// add other sql statements here
EOSQL
FROM mdillon/postgis:9.6-alpine
COPY initdatabase.sh /docker-entrypoint-initdb.d/

pgAdmin4: Unable to connect to Amazon EC2 via SSH Tunnel

I have Amazon EC2 instance running Ubuntu. I have installed and configured PostgreSQL.
Contents of the file /etc/postgresql/9.3/main/pg_hba.conf:
local all all md5
host all all 0.0.0.0/0 md5
Also in postgresql.conf
I have set listen_addresses='*'.
The test command below is successfully starting psql console.
psql -U postgres testdb
Now I am trying to connect pgAdmin4 from MacOS.
I have created a SSH tunnel with following command:
ssh -i ~/.ssh/test.pem -fN -L 5433:localhost:5432 ubuntu#mytestdomain.com
Now I have following details in pgAdmin:
When I save, I get this output:
Unable to connect to server: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.
What am I doing wrong ?
Here is the solution,
install pgadmin 4 into your system. and configure the below-added configurations. if the below configurations do not work then please check that DB user permissions on AWS. because of the restriction on IP level so it may not be able to access.