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