Can't connect remotely to PostGIS docker - postgresql

I've created a PostGIS docker container with the following code:
docker run --name=h4d -d -e POSTGRES_USER=h4d_user -e POSTGRES_PASS=password -e POSTGRES_DBNAME=gis -e ALLOW_IP_RANGE=0.0.0.0/0 -p 5432:5432 -v h4d_data:/var/lib/postgresql --restart=always kartoza/postgis:latest
I can connect to the docker from my localhost, but I can't from another terminal. The error message says "could not connect to server: Connextion timed out (0x0000274C/10060) Is the server running on host "" and accepting TCP/IP connections on port 5432?
I'm not sure if maybe I must edit some firewall settings or something else. I'm working on Windows 10

Maybe this will help ..
Go to the postgresql.conf and change the parameter of listen_addresses to the ip address you wish or just place * for all ips, e.g:
listen_addresses = '*'
In the pg_hba.conf file you also have to add which ips and users may access a certain database, e.g.
host my_db my_user 128.176.1.1 md5
In an Ubuntu machine these files are normally found at: /etc/postgresql/10/main

Related

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 the Pact Broker to a local or cloud instance of PostgreSQL?

I'm struggling to get the Pact Broker running in a docker container to connect to my local installation of PostgreSQL on Windows.
This is what my docker run command looks like...
docker run pactfoundation/pact-broker
-e PACT_BROKER_DATABASE_USERNAME=pact_broker
-e PACT_BROKER_DATABASE_PASSWORD={Password}
-e PACT_BROKER_DATABASE_HOST=host.docker.internal
-e PACT_BROKER_DATABASE_NAME=pact_broker
-e PACT_BROKER_DATABASE_PORT=5432
-p 9292:9292
Which returns the following error...
Unable to load application: Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
bundler: failed to load command: puma (/pact_broker/vendor/bundle/ruby/2.6.0/bin/puma)
Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Here is my database and user in pgAdmin 4
Here is the command I'm using to test my connection
psql postgresql://pact_broker:{Password}#localhost/pact_broker
Which returns the following...
psql (13.3)
WARNING: Console code page (65001) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
pact_broker=>
I've also tried running the following command which returns the same error
docker run pactfoundation/pact-broker -e PACT_BROKER_DATABASE_URL=postgresql://pact_broker:{Password}#host.docker.internal/pact_broker -p 9292:9292
My postgres.conf file contains listen_addresses = '*' and my pg_hba.conf contains the following
# 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 all all 0.0.0.0/0 trust
host replication all ::1/128 scram-sha-256
host all all ::/0 md5
I'm having similar problems with an instance hosted with Azure Database for PostgreSQL
My connection string looks like the following...
psql "host=pactbrokerserver.postgres.database.azure.com port=5432 dbname={your_database} user=postgres#pactbrokerserver password={your_password} sslmode=require"
Which I've attempted to connect to via the pact broker locally with...
docker run pactfoundation/pact-broker
-e PACT_BROKER_DATABASE_USERNAME=postgres#pactbrokerserver
-e PACT_BROKER_DATABASE_PASSWORD={Password}
-e PACT_BROKER_DATABASE_HOST=pactbrokerserver.postgres.database.azure.com
-e PACT_BROKER_DATABASE_NAME=pactbroker
-e PACT_BROKER_DATABASE_PORT=5432
-e PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES=5
-p 9292:9292
And it returns exactly the same error.
What could be the issues here?
I think what's happening here is that you've put the container name before the environment argument list to the docker run command.
So instead of setting the PACT_BROKER_DATABASE_NAME and other environment variables for the running container with your custom values, they are simply being discarded by the runtime.
Try this instead:
docker run -e PACT_BROKER_DATABASE_USERNAME=pact_broker \
-e PACT_BROKER_DATABASE_PASSWORD={Password} \
-e PACT_BROKER_DATABASE_HOST=host.docker.internal \
-e PACT_BROKER_DATABASE_NAME=pact_broker \
-e PACT_BROKER_DATABASE_PORT=5432 \
-p 9292:9292 \
pactfoundation/pact-broker
Debugging Options
In case I'm wrong, it seems likely that the issue could be network path related. To rule this out, we can get a shell into the docker container by replacing the entrypoint (which is usually the script to start and run the web server) with /bin/sh:
docker run -it --entrypoint /bin/sh --user root -e PACT_BROKER_DATABASE_USERNAME=pact_broker -e PACT_BROKER_DATABASE_PASSWORD=Penwater858 -e PACT_BROKER_DATABASE_HOST=host.docker.internal -e PACT_BROKER_DATABASE_NAME=pact_broker -e PACT_BROKER_DATABASE_PORT=5432 -p 9292:9292 pactfoundation/pact-broker
# once in the container...
apk update
apk add busybox-extras
# see if there is a network path
telnet $PACT_BROKER_DATABASE_HOST $PACT_BROKER_DATABASE_PORT

Docker Rundeck + local Postgres

I'm trying to run a docker image of rundeck, using PostGres for the database.
The issue I'm having is mapping my local postgres installation to the docker rundeck image. The postgres port runs on 5432, and have confirmed using netstat that the port is open and listening. The port for rundeck needs to run on 4440.
I have tried the following command:
docker run -p 127.0.0.1:4440:4440 -e RUNDECK_DATABASE_URL=jdbc:postgresql://localhost/rundeck -e RUNDECK_DATABASE_DRIVER=org.postgresql.Driver -e RUNDECK_DATABASE_USERNAME=xxx -e RUNDECK_DATABASE_PASSWORD=xxx --name test-rundeck -t rundeck/rundeck:3.0.19
But it fails with an error: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP
I'm guessing it's because the internal 5432 port isnt mapped to the docker container port 5432?
I tried mapping the ports with -p 127.0.0.1:5432:5432 but that fails with the error:
Error starting userland proxy: listen tcp 127.0.0.1:5432: bind: address already in use
At this point I might just resort to running both PostGres and Rundeck as docker images, but I would rather like to resolve this problem.
Any ideas on how to map a local PostGres to a docker ran Rundeck?
Found the answer.
Had to edit the pg_hba.conf file to allow the docker0 ip address through.
host all all 172.23.0.0/16 md5

PostgreSQL: Remotely connecting to Postgres instance using psql command

I want to remotely connect to a Postgres instance. I know we can do this using the psql command passing the hostname
I tried the following:
psql -U postgres -p 5432 -h hostname
I modified the /etc/postgresql/9.3/main/pg_hba.conf file on the target machine to allow remote connections by default
I added the following line to the file
host all all source_ip/32 trust
I restarted the cluster using
pg_ctlcluster 9.2 mycluster stop
pg_ctlcluster 9.2 mycluster start
However, when I try to connect from the source_ip, I still get the error
Is the server running on host "" and accepting TCP/IP connections on port 5432?
What am I doing wrong here?
I resolved this issue using below options:
Whitelist your DB host from your network team to make sure you have access to remote host
Install postgreSQL version 4 or above
Run below command:
psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>
psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W
-W option will prompt for password. For example:
psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W
I figured it out.
Had to set listen_addresses='*' in postgresql.conf to allow for incoming connections from any ip / all ip
Step Wise below
Opening the Port - Make sure the PSQL Port is open to all remote connections or connections from a specific set of IPs as per your requirement. PSQL, in general, runs at port 5432, and it is configurable, so expose relevant Port accordingly.
Update Remote Server PSQL Configuration - Set listen_addresses = '*' in postgresql.conf file, path in general is /etc/postgresql/psql_version/main/postgresql.conf
Connect remotely - psql -U <db_username> -h <IP_address> - in case psql is running on a port other than 5432 on the remote server, specify port by adding -p <port_number>
A little plus below -
In case the IP has been mapped to a domain name, you can connect by replacing <IP_address> with <host_name>. To do this, add a new connection rule in pg_hba.conf file
Note -
All above explained can cause security issues - best practice always is to either keep your psql port closed, or only allow a list of IPs to connect through the port.
Note that "ident" in pg_hba.conf requires a "ident server" to be running on the client.

psql cant connect to PostgreSQL server (postmaster) on IP and port 5432?

Please read before replying it as duplicate (as it perhaps can happen). I am running my postmaster (postgres) server. See below for 'sudo netstat -anp|grep 5432' output?
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 29606/postmaster
unix 2 [ ACC ] STREAM LISTENING 1650581 29606/postmaster /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 1650582 29606/postmaster /tmp/.s.PGSQL.5432
I am able to connect from localhost using
psql -h localhost (OR 127.0.0.1) -d <DB> -U user -W
But when I try to connect from other hosts using tcp, by specifying
psql -h ip_add_postmaster -d <DB> -U user -W
It throws:
psql: could not connect to server: Connection refused
Is the server running on host XXXXXX and accepting TCP/IP connections on port 5432?
What's wrong here?
pg_hba.conf
# 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
In postgresql.conf,
listen_addresses = 'localhost, 127.0.0.1, ip_add_postmaster'
Note: ip_add_postmaster is same as my Elastic IP and not public DNS. If this information
matters.
What am I doing wrong here? Machine is hosted on Amazon EC2 and have open the port 5432.
As your netstat output indicates, it's listening at 127.0.0.1:5432 which is localhost. That is only connectable from localhost ;)
Set listen_addresses='*' in your config and it will work.
[edit]
Other things to check:
is the amazon firewall blocking anything?
is iptables blocking anything?
But first make sure the listening address is correct, your netstat output shows that it won't work like this.
listen_addresses='localhost, private_ip' fixed the issue. I was not able to start postmaster server on elastic IPs. Once postgres server started o localhost and private IPs, I was able to connect.
One other issue I have found was if you end up with two Postgres installations, the second one can choose non-default port (in my case it was 5433 i/o 5432). So checking the port in postgresql.conf might be a good idea.
I ran into this issue and tried all sorts of fixes I found across SO, and want to add a simple solution that worked for me after realizing it had to do with permissions in my case.
Simply, if you're running a psql server on Windows, you are initially restricted to the default postgres superuser for logging in, launching the server, and so on.
So, first try running from the command line:
psql -U postgres -h localhost -p 5432
and enter your password at the prompt. If you've managed to login and the server is up, then it was a permissions issues. From here, you can create a role for yourself that has login privileges to whatever database you are trying to run.
If the error persists, then consider checking postgresql.conf as mentioned above, to make sure default IP is set to * or localhost, and the port set to 5432 or whatever port you want as default.
I also ran into the same issue. On debugging, it was nothing related to the port, but due to some missing directories in the Postgres folder.
While updating Mac OS (from 10.13.1 -> 10.13.13), some folders in the directory /usr/local/var/postgres/ gets deleted. The fix was the adding the missing directories:
mkdir /usr/local/var/postgres/pg_tblspc
mkdir /usr/local/var/postgres/pg_twophase
mkdir /usr/local/var/postgres/pg_stat
mkdir /usr/local/var/postgres/pg_stat_tmp
mkdir /usr/local/var/postgres/pg_replslot
mkdir /usr/local/var/postgres/pg_snapshots
mkdir /usr/local/var/postgres/pg_logical/{snapshots,mappings}