docker app postgres local connection - postgresql

I have created a docker image with playframework backend that connects to pgsql localhost database. It is running smoothly when I run it in my local machine but when I run it in docker I receive errors.
I want to connect the docker container to my posgresql localhost.
I have tried configuring postgresql.conf and pg_hba.conf to open the port 5432
pg_hba.conf
# TYPE DATABASE USER ADDRESS
METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.0/24 trust
host all all 172.17.0.0/16 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
host all all ::/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all 172.17.0.0/32 trust
host replication all ::1/128 trust
postgresql.conf
listen_addresses = '*'
# what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432
When running my playframework app. I am getting the error below
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
On a side note, postgresql is installed using brew, running correctly can access database using pgadmin4.

Related

Docker Postgresql remote access

PostgreSQL 13 is running on a remote machine in a docker container. I can't get access to it remotely in any way, the error is
no pg_hba.conf entry for host,
ssl is disabled.
Connect from the local machine by the host name localhost or 127.0.0.1 is excellent. But if I set a own IP of the current server 192.168.1.102 - I am getting the error. And from any remote machine it is the same
postgresql.conf
listen_addresses = '*'
pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all all trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all ::1/128 trust
host all all all md5
I did a container restart and the postgres user has a password
Thanks
Try use
docker run --publish=192.168.1.102:<exposed-port>:<container-port> postgresql-image
Also make sure you can ping to IP from other PC. Firewalls, Virus guards etc should check.
There are a great number of solutions described here

After changing the port number, connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"

I finished install postgresql11 and to change port number
The first file I modified was '/var/lib/pgsql/11/data/postgresql.conf'
#listen_addresses = 'localhost' -> listen_addresses ='*'
#port = 5432 -> port = 9485
The second file I modified was '/var/lib/pgsql/11/data/pg_hba.conf'
the line i added is 'host all all 0.0.0.0/0 md5'
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
i restarted postgresql server
systemctl restart postgresql-11
Connection from external client to dbeaver works fine.
but local connection is not fine
su - postgres
psql
there is error message
psql: could not connect to server: There is no such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I can find two files in '/var/run/postgresql'
.s.PGSQL.9485
.s.PGSQL.9485.lock
If Ireturn the port to the original number of 5432, it works normally again.
Please tell me how to fix it
psql is not aware of your Postgres settings, it is using the default port 5432.
Try: psql -p 9485
To complete your setup you have to change environment variable PGPORT
PGPORT=9495; export PGPORT;
If not you have to give port number ( -p 9495 ) to every cde (psql, pg_dump, ...)

pgAdmin 4 unable to connect to server: timeout expired

I want to connect to a pgAdmin 4 database by a remote server. However, when I want to save the connection properties, I get the following message:
unable to connect to server: timeout expired
This the content of the "pg_hba.conf":
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
listen_addresses in "postgresql.conf" is also set to '*'
What should I do to be able to connect to the server remotely?

Unable to connect to an postgresql ec2 instance from pgadmin4

I created an EC2 instance and then installed postgresql (version 11) and timescaledb in it. My security group looks as follows:
As of now, I have enabled this instance for all the IP addresses.
My /etc/postgresql/11/main/pg_hba.conf looks like this:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all 0.0.0.0/0 md5 #ipv4 range
host all all ::0/0 md5 #ipv6 range
host all all all md5 #all ip
and the /etc/postgresql/11/main/postgresql.conf looks like this:
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
Then, I try to connect to the database via Pgadmin4 using the Public DNS(IPv4):ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com
After I enter the username and password, it throws the following error:
Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com" (xx.xxx.xxx.xxx) and accepting
TCP/IP connections on port 5432?
I had a look at the following post and followed all the instructions, but to no avail:
connect-to-remote-postgresql-server-on-amazon-ec2
Can anyone please help me out with this?
Edit 1
When I do:
/etc/init.d/postgresql restart
I get:
[....] Restarting postgresql (via systemctl): postgresql.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'postgresql.service'.
Authenticating as: Ubuntu (ubuntu)
Password:
I don't know what password to enter here so that the postgresql can get restarted
As pointed out by #jjanes in the comments, I had to restart the postgresql inorder to access it.
Doing
/etc/init.d/postgresql restart
was throwing an error (as shown in the question) that's why I had to use
sudo /etc/init.d/postgresql restart
in order to grant the root access.
Once the db was restarted, I tried connecting to the ec2 instance again via pgadmin4, and this time it worked.

Correct setings for pg_hba.conf

Having difficulty getting Mezzanine to connect to Postgresql via Docker
In my postgresql.conf I have:
listen_addresses = '*'
In my pg_hba.conf I have:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
host all all samenet trust
host all all 0.0.0.0/0 md5
The error I keep getting is:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
I have PORT in settings.py set to '' but I have PORT defined in postgresql.conf to 5432.
Any suggestions?
Ensure that port mapping is properly configured https://docs.docker.com/userguide/dockerlinks/
If your app is running in one container and postgresql server is running in another container, you need to link containers together.
By the way you are trying to establish connection via localhost and you have md5 authentication method there. For debug purposes change authentication method to trust. You should EDIT this line in your pg_hba.conf NOT to ADD another line.
host all all 127.0.0.1/32 md5
The best way to debug connection issues is to use psql utility.