Docker container, running PostgreSQL not allowing php's pg_connect from another container - postgresql

I have the following architecture:
A network with 3 containers:
container_db, running PostgreSQL
container_pg_admin, running a simple pgAdmin environment
container_php_dev, running the whole environment, needed for the application to function properly (nginx, php, angular for the frontend, etc.)
When I try to pg_connect() from container_php_dev to container_db the connection gets refused. This is the actual error message I get: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: No route to host↵ Is the server running on host "container_db" (172.18.0.4) and accepting↵ TCP/IP connections on port 5432?.
However, when I try to connect to container_db from container_pg_admin I face no problem achieving this (host is container_db and port is 5432). Even if I map port 5432 of container_db to 5555 on host, I'm able to connect to 127.0.0.1:5555 from DBeaver or other DB Manager (TeamSQL).
All containers were restarted a few times as well as docker itself. And as this is my development machine, the host PC was restarted as well.
What might be the problem, how to diagnose and possibly solve? Have any of you experienced such strange docker behaviour?

Try running the container_db in the hosted network using flag --network host

Related

I cannot connect local pgadim to a container that runs postgres

i have an issue
i have a container that has a web application running and a postgres db, when i launch the webapp is connected in database correctly, but when in local i tried using a pgadmin that is install on my machine and i try to connect to database of container it gives me an error connection
error message
TIPS
the container has the port-forward of database
i tried using both localhost and the ip of container as host when initialize connetion using pgAdmin
I have no idea how to do that, someone can help me

Connection problem with postgresql and docker

I'm running a composition of docker containers of a preexisting project on Ubuntu 20. One container for apache2, one for postresql. I can connect to the webserver through my browser and the webserver container establishes a connection to the postgresql container - so far everything works.
The problem is: I can't establish a connection from my host machine to the postgresql database using php. Whenever I try, I get an password authentication failed for user .. error.
I know that the php code, the username and the password are correct, as all three work when I execute them from within the webserver container.
$db_connection = pg_connect("host=db dbname=MYDB user=MYUSER password=MYPASS");
I configured /etc/hosts on my host to resolve "db" to 127.0.0.1 and also tried using host=localhost. Both didn't help.
Am I getting something wrong about how docker works? I found a couple of OSX solutions which were talking about docker containers running in virtual machines and therefore not being accessible from the host... Which is confusing, as apache2 is reachable from my host.
I did my research on stackoverflow tried solutions from e.g. Connecting to Postgresql in a docker container from outside but they didn't work for me.

Docker: Unable to connect to a remote Postgres server from a Docker Container

I installed the Metabase application, created a Docker container (on one Linux host) and I want to connect to a remote Postgres database (on a second Linux host) from the Docker container. The setup of Metabase allows entry of connection parameters to the Postgres database. Each time I enter the proper connection parameters in Metabase I get a database connection error. I can connect to the Postgres database from the host running the container using PSQL with no problem. My question is, is there something within Docker or the remote Postgres server that I must setup in order to allow a connection between a Docker container and a remote database? I realize this is normally caused by incorrect host/port information, problems with DNS, firewall blocking or other network problems. I am new to Docker and I do not know where to look or what to setup to make this work. Does anyone have suggestions about how to make this connection work? Thank you!

How to Connect using Port Forwarding Database Postgrsql on Openshift 3

I Have a problem on Connect from Port Forwarding Database on Openshift :
Running Pods Postgresql :
I Try Connect to Container running the database to check process and psql command, then it works :
Next, I Try Port Forwarding for Try Connection from outside Openshift Cluster:
Then I Try Connect from Outside Cluster to connect Postgresql have Error: Connection Refuse
Im Using IP Based or Hostname / FQDN Not Working and Error Still Exist
And When I Try Check Firewall port it has been opened port 5432/TCP :
Anyone Can Help Me With This problem ?
Thanks
Note: Before I have Been Looking Documentation but Not Working Resolve the Problem
Source Documentation:
https://www.openshift.com/blog/openshift-connecting-database-using-port-forwarding
"psql: could not connect to server: Connection refused" Error when connecting to remote database
The oc port-forward command is forwarding from only your loopback interfaces.
If you are running your client on the same machine where the cluster is running, then use localhost as your "Host".
If you are running your client on a different machine, they you need more network redirection to get this to work. Please see this post for more information as well as work-arounds for your problem: Access OpenShift forwarded ports from remote host

HEROKU POSTGRES error: Connection refused Is the server running on host "..compute-1.amazonaws.com..." and accepting TCP/IP connections on port 5432?

I have been trying for weeks to figure out what's the issue, but I cannot seem to fix this.
I am developing a web app, and I have deployed to production using Heroku. I'm using Heroku Postgres for my database, and I'd like to psql into that hosted database from my local machine to make changes to it. I have tried from both Mac OS X and Linux Mint.
However, no matter what I try, I keep getting the following error:
---> Connecting to HEROKU_POSTGRESQL_ORANGE_URL (DATABASE_URL)
psql: could not connect to server: Connection refused
Is the server running on host "ec2-***-**-***-**.compute-1.amazonaws.com" (***.**.***.**) and accepting
TCP/IP connections on port 5432?
I'm starting to think that I may have to SSH into the Amazon EC2 instance and change the Postgres configuration? Would that even be possible?
You can't connect directly to the EC2 instance where the Heroku Postgres instance is running; it is managed by Heroku and normal users aren't allowed to connect to it.
If your application is running fine in Heroku, and you're running heroku pg:psql to attempt to connect to the DB, then most likely there is some issue with your local network that is preventing you from connecting to the Postgres database, like a firewall blocking outbound connections. Heroku Postgres databases are open to the public without any restriction on IP ranges.
It could be a problem with the proxy or firewall of the internet connection you are using.
Try to connect your PC to your mobile phone tethering and then launch the command again, just to verify if the problem lies there.
I was getting this kind of error when my pg_hba.conf or AWS security groups have not been configured properly. There are plenty of docs about how to resolve this. For example, you can check this list link
You can directly connect to EC2 Heroku Postgress instance by adding this attributes to the end of your DB connection url:
jdbc:postgresql://<host>/<dbname>?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory