Docker container connecting to Postgres database - postgresql

I am receiving the following error when connecting my Django docker container to my Postgres database.
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line
130, in connect conn = _connect(dsn,
connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: 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?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Below is my Dockerfile which run the container
FROM python:3.6
MAINTAINER c15523957
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y install libgdal-dev
RUN mkdir -p /usr/src/app
COPY requirements.txt /usr/src/app/
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Here is the code in my settings.py file of my Django code
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'place_loc_db',
'USER': 'place_loc_user',
'PASSWORD': 'abc123',
'HOST': 'localhost',
'PORT': 5432,
}
}
Note: I do not have the postgres database running as a container but on my local computer
Note: The following details are contained in my pg_hba.conf and postgresql.conf files
postgresql.conf- > listen_addresses = '*'
pg_hba.conf - > host all all 0.0.0.0/0 md5
I've read that the following details above open connections on the database.

It looks like you're trying to connect to localhost. Your Postgres database is notrunning inside the same container as your django app, so you're not going to be able to find it at localhost. You need to point your app at the address of the Postgres container.
If you run your containers in a user defined network (using docker-compose will do this for you automatically), then you can use container name as hostnames.
The documentation on container networking is a good place to start.
Update
The fact that you're running Postgres on your host doesn't substantially change the answer: you still need to point your webapp at the address of the Postgres server, rather than localhost.
The easiest way of doing that depends on whether or not you're running Docker natively on Linux, or you're running Docker-For-X, where X is MacOS or Windows.
On Linux, just point your webapp at the ip address of the docker0 interface. This is an address of your host, and since you have Postgres configured to listen on all interfaces, this should work out just fine.
If you're on Mac or Windows, there is a special "magic hostname" that refers to services on your host. E.g., read this for details under MacOS. In both cases, you can point your webapp at host.docker.internal.

Make sure this is done
postgresql.conf- > listen_addresses = '*'
pg_hba.conf - > host all all 0.0.0.0/0 md5
brew services start postgresql (In MAC, reload postgres Service ).
Get your system Ip.
Use your system ip address instead of localhost in your DB connection.

Related

Connect to existing instance of Postgresql from WSL 2

I'm running Ubuntu WSL 2 on Windows 10. Before I installed WSL I already had Postgresql installed on my Windows 10. I wanted to connect to the database from WSL but so far it fails.
When running:
psql
I get:
psql: error: 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?
When running:
psql -h 127.0.0.1 -p 5432 -U postgres
I get:
psql: error: 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?
And WSL clearly doesn't see the database. When running this:
sudo service postgresql start
It returns:
postgresql: unrecognized service
I know that one solution is to install the database in WSL but wanted to try to connect to existing instance first.
You should edit pg_hba.conf
check wsl subnet
execute ipconfig from cmd. Below is example.
Ethernet Adapter vEthernet (WSL)
IPv4 Address . . . . . . . . . . . .: 172.19.230.81
edit pg_hba.conf
pg_hba.conf path is C:\Program Files\PostgreSQL\{postgresqlVersion}\data\pg_hba.conf. if you installed Windows Postgresql default install path.
subnet mask is use value from ipconfig
host all all 172.19.230.0/24 md5
restart postgresql
you can restart postgresql from services.msc. service name is postgresql-x64-{postgresqlVersion}.
access from psql
psql -h 172.19.230.81 -p 5432 -U postgres

Can't connect remotely to PostGIS docker

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

Cant access database from pgAdmin

I`m using postgresql database on my web server (ubuntu + nginx) and it works with backend app. I want to connect remotely using pgAdmin
but it always throw this error:
could not connect to server: Connection timed out(0x0000274C/10060)
Is the server running on host "ip" and accepting
TCP/IP connections on port 5432?
I already add to my pg_hba.conf this line:
host all all all password
and also add to postgresql.conf line:
listen_addresses = '*'
netstat -nlt does next result, and it`s enough in tutuorials i found, but still cant access my database
Found a solution, it works as well after this command:
sudo ufw allow 5432/tcp
Open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart

Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432

I install pgadmin4 using the following command sudo install pgadmin4 and then I installed postgresql using sudo install postgresql . In my terminal I ran the command psql -U postgres -h localhost
It shows connection refuse
Then I go to pgadmin4 and created a server and I wanted to connect to the server it shows
pgadmin connection refuse
Then I wrote host all all all md5 in pg_hba.conf file and I wrote listen_addresses = '*' to postgresql.conf file. and I wrote the following command sudo service postgresql restart but again It shows connection refuse message.
I wrote psql and it shows connections on Unix domain socket
Can you please help me in this regard? What I have missed?

PostgreSQL docker: "could not bind IPv6 socket: Cannot assign requested address"

EDIT 2: After a long time, solved! See answer below.
EDIT: I am sorry to say that the the problems went away "on their own" between yesterday and today, without me having done anything. Great non-deterministic lesson to learn here...
Bonus fun: the "could not bind IPv6 socket" error still appears in the error logs, so this was probably not even the problem in the first place.
I have a problem with a previously functioning docker PGSQL image. Until an uninspired rebuild yesterday ( :-D ), I've used this build successfully for the last 5+ months.
My system:
Ubuntu 17.04 64b
PGSQL 9.6.4
Docker version 17.11.0-ce, build 1caf76c
I am mapping host port 5433 to container port 5432
The problem (snippet from the PGSQL logs):
...
LOG: could not bind IPv6 socket: Cannot assign requested address
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
...
This is what ss is telling me (when the container is running, obviously):
$ docker exec -it db ss -atune
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp ESTAB 0 0 127.0.0.1:45876 127.0.0.1:45876 uid:999 ino:147509 sk:00000001 <->
tcp LISTEN 0 128 127.0.0.1:5432 *:* uid:999 ino:147500 sk:00000002 <->
I've cleaned all docker containers / images, I've reinstalled docker, nothing helped. Who can possibly be using the 5432 port in the container?
For that matter, am I reading this correctly, that PGSQL is complaining about the 5432 port being already used in the docker container?
Even if you have no solution, a basic idea of how to proceed with debugging this would be a great help.
EDIT:
postgres.docker file
FROM postgres:9.6.4
ADD bin/postgres-setup.sh /docker-entrypoint-initdb.d/postgres-setup.sh
RUN chmod 755 /docker-entrypoint-initdb.d/postgres-setup.sh && \
apt-get update && \
apt-get install -y --no-install-recommends postgresql-plpython3-9.6 python3-pip postgresql-9.6-pldebugger && \
pip3 install pyexcel pyexcel-xls pyexcel-xlsx pyexcel-xlsxw
After a long time, we finally figured out what was the problem -- adding explanation here, in case it helps others.
Since pgsql listens only to localhost by default, when it is running in the docker container, where we have the port mapping configuration, the external API was not able to connect to the pgsql server.
The solution is to allow pgsql to listen to all IP addresses:
Connect to db container shell: $ docker exec -ti db bash
Change the configuration file /var/lib/postgresql/data/postgresql.conf to allow pgsql to listen to all IPs: listen_addresses = '*'
Certain edits have to be made to the postgres.conf and pg_hba.conf files in order for Postgres inside the container to listen to connections from the host:
See the Gotchas section at the cityseer/postgis repo.
Check that your postgresql.conf file has the listen_addresses item uncommented and set to listen to all ports, i.e. listen_addresses = '*';
Check that your pg_hba.conf file allows the docker container's Postgres to provide local and host access per the following two lines:
local all all trust
host all all 0.0.0.0/0 trust