Cannot connect to postgres server in docker - postgresql

I started a postgresql server in docker and exposed the 5432 port by sudo docker run -it -p 5432:5432 9c421f1a239c bash and start the postgres server manually inside the docker container, but cannot connect to it with command: psql -h 172.17.0.63 -U venti. 172.17.0.63 is a right IP, and venti is my pg username. But get error:
psql: could not connect to server: Connection refused
Is the server running on host "172.17.0.63" and accepting
TCP/IP connections on port 5432?
My pg_hba.conf looks like this:
local all postgres peer
host all all 0.0.0.0/0 trust
local all all trust
Connecting to pg server inside container works successfully.
Dockerfile:
FROM ubuntu:12.04
RUN apt-get update
RUN apt-get install -y gcc libc-dev-bin libc6 libc6-dev libssl-dev libkrb5-dev comerr-dev
RUN apt-get install -y postgresql-common libpq-dev postgresql-9.1-postgis --fix-missing
RUN apt-get install -y postgresql postgresql-client
USER postgres
ENV PGDATA /etc/postgresql/9.1/main
ENV LOGDIR /etc/postgresql/9.1/main/postgresql.log
WORKDIR /usr/lib/postgresql/9.1/bin
USER root
RUN apt-get install -y vim
USER postgres
RUN sed -e '90d' -i /etc/postgresql/9.1/main/pg_hba.conf
RUN sed -e '91d' -i /etc/postgresql/9.1/main/pg_hba.conf
RUN echo "host all all 0.0.0.0/0 trust" >> '/etc/postgresql/9.1/main/pg_hba.conf'
RUN echo "local all all trust" >> '/etc/postgresql/9.1/main/pg_hba.conf'
RUN ./pg_ctl start && sleep 8 && ./createdb pg && ./createdb bloodstone \
&& createuser -Upostgres -s venti \
&& createdb -Uventi -Oventi venti
# ENTRYPOINT ./pg_ctl start && bash -c "while true; do echo "" > /dev/null; sleep 1; done"
VOLUME $PGDATA
EXPOSE 5432

It's merely a misconfiguration. I should have set pg to listen on public addresses or make a port mapping. I fixed this by editing pg config file with sed:
RUN sed -e "s/[#]\?listen_addresses = .*/listen_addresses = '*'/g" -i '/etc/postgresql/9.1/main/postgresql.conf'
Add this to a proper place in your Dockerfile, and you should be OK.

To troubleshoot postgres auth problems in general, look at the postgres log/stderr to see verbose reasons why it failed. (I see your problem is solved but if anyone runs into similar problems)
To find the postgres log location: "show log_destination;" if you have a working psql (eg locally on the postgres server box)
I see yours is set to "/etc/postgresql/9.1/main/postgresql.log". You can connect to the container to see the log with "docker exec -it container_name bash".
Alternately, if a container runs postgres directly, "docker attach db_container_name" views postgres stderr messages.
Note that by default user postgres does not have a password and uses only ident auth.

I also faced similar issue. Try running docker-compose up twice.
The reason is, that in my docker-compose.yml file the DB service was below the Web service in which it was trying to connect to DB which does not exists yet.
**
version: '2'
services:
nginx:
image: nginx:latest
container_name: ng01
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static <--- HERE
depends_on:
- web
web:
build: .
container_name: dg01
command: bash -c "python /src/IMS/manage.py makemigrations && python /src/IMS/manage.py migrate && gunicorn IMS.wsgi -b
0.0.0.0:8000"
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
db:
image: postgres:latest
container_name: ps01**

Related

Docker container has no IP, but no network flags were set [duplicate]

I have Postgresql on a server in a docker container. How can I connect to it from the outside, that is, from my local computer? What setting should I apply to allow that?
You can run Postgres this way (map a port):
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
So now you have mapped the port 5432 of your container to port 5432 of your server. -p <host_port>:<container_port> .So now your postgres is accessible from your public-server-ip:5432
To test:
Run the postgres database (command above)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05b3a3471f6f postgres "/docker-entrypoint.s" 1 seconds ago Up 1 seconds 0.0.0.0:5432->5432/tcp some-postgres
Go inside your container and create a database:
docker exec -it 05b3a3471f6f bash
root#05b3a3471f6f:/# psql -U postgres
postgres-# CREATE DATABASE mytest;
postgres-# \q
Go to your localhost (where you have some tool or the psql client).
psql -h public-ip-server -p 5432 -U postgres
(password mysecretpassword)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mytest | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres
So you're accessing the database (which is running in docker on a server) from your localhost.
In this post it's expained in detail.
I managed to get it run on linux
run the docker postgres - make sure the port is published, I use alpine because it's lightweight.
docker run --rm -P -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234" --name pg postgres:alpine
using another terminal, access the database from the host using the postgres uri
psql postgresql://postgres:1234#localhost:5432/postgres
for mac users, replace psql with pgcli
You can also access through docker exec command by:
$ docker exec -it postgres-container bash
# su postgres
$ psql
Or
$ docker exec -it postgres-container psql -U postgres
I am using django with postgres in Docker containers. in the docker-compose file, add the following:
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
ports:
- "6543:5432"
This ports setting uses the port 6543 (it just needs to be different from 5432) that is accessible by your local machine. For myself, I connected DBeaver to it. this will prevent port clashes between your app request and local machine request.
At first, I got a message saying that the port 5432 is in use (which is by django app) so I couldn't access by pgAdmin or DBeaver.
I'm assuming that you want to be able to view data present in your container everytime you connect to it from outside. To do this, you will have to persist data on the postgres image.
If you dont have persistant data, you will have to repeat everything you did the first time.
Steps 3, 5, 6, 7, and 8 answer your question directly.
Here is the detailed overview of the entire process I followed on Windows 10 powershell (commands are the same in Linux and macOS as well):
Step 1: Start powershell in non-admin mode
Step 2: Download postgres docker image:
docker pull postgres:latest
Step 3: Start docker container in detached mode and persist data on postgres image by creating a volume and binding it to a destination
(Note: by default 5432 is the default port that is used; but state it explicitly to prevent connection errors from clients like pgadmin, dbeaver, etc.)
docker run --name postgres-test -e POSTGRES_PASSWORD=password -p 5432:5432 -v postgres-data:/var/lib/postgresql/data -d postgres:latest
Step 4: Check status of running containers
docker ps -a
Step 5: Go inside container_name in interactive mode
(Note: commands like ls, pwd, etc. can be executed here if you've checked linux containers during installation)
docker exec -it postgres-test psql -U postgres
Step 6: Create sample data. At this point, you can play with psql commands in the following manner:
# CREATE DATABASE test;
# \c test
# CREATE TABLE test_table(something int);
# INSERT INTO test_table VALUES (123);
# SELECT * FROM test_table;
# \q
Step 7: Open a database client application like pgadmin or dbeaver and enter the below in the connection fields:
Host: localhost
Database: test
User: postgres
Password: password
Step 8: Enter the query select * from test_table in the query editor and you should be able to see the output 123
I know this is late, if you used docker-compose like #Martin
These are the snippets that helped me connect to psql inside the container
docker-compose run db bash
root#de96f9358b70:/# psql -h db -U root -d postgres_db
I cannot comment because I don't have 50 reputation. So hope this helps.
I already had running postgres on host machine and didn't want to allow connections from network, so I did run temporary postgres instance in container and created database in just two lines:
# Run PostgreSQL
docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres
# Create database
docker exec -it postgres-container createdb -U postgres my-db
For some reason 5432 port seems protected. I changed my port config from 5432:5432to 5416:5432 and the following command worked to connect to your postgres database from outside its docker container:
psql -h localhost -p 5416 -U <my-user> -d <my-database>
To connect from the localhost you need to add '--net host':
docker run --name some-postgres --net host -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
You can access the server directly without using exec from your localhost, by using:
psql -h localhost -p 5432 -U postgres
Connect to a local container running postgres
Install psql
brew search postgres
brew install postgresql
2.
docker run --name postgres -e POSTGRES_DB=users \
-e POSTGRES_USER=john \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 -d postgres
psql --host=localhost --username=john --dbname=users
I tried to connect from localhost (mac) to a postgres container. I changed the port in the docker-compose file from 5432 to 3306 and started the container. No idea why I did it :|
Then I tried to connect to postgres via PSequel and adminer and the connection could not be established.
After switching back to port 5432 all works fine.
db:
image: postgres
ports:
- 5432:5432
restart: always
volumes:
- "db_sql:/var/lib/mysql"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres_db
This was my experience I wanted to share. Perhaps someone can make use of it.
first open the docker image for the postgres
docker exec -it <container_name>
then u will get the root --root#868594e88b53:/#
it need the database connection
psql postgresql://<username>:<databasepassword>#postgres:5432/<database>
This one worked for me:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres
Use the above to load an initial script as:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres < src/sql/local/blabla.sql
Do not that i remap my ports as:
docker run -p3307:5432 --name postgres -e POSTGRES_PASSWORD=postgres -d postgres
In case, it is a django backend application, you can do something like this.
docker exec -it container_id python manage.py dbshell
After building my gateway-microservice application i had the same issue. Can not to connect to contenerized postgresql from Heidisql.
At this moment i have solved it by simply specifying postgresql password to docker-compose.yml as well as port.
So you should find and open docker-compose.yml. Then you should enter POSTGRES_PASSWORD (don`t let it to be empty), and specify the port “5432:5432”
services:
microservice33-postgresql:
environment:
- POSTGRES_USER=microservice33
- POSTGRES_PASSWORD=wwww
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- 5432:5432
link for reference and screenshots post
There are good answers here but If you like to have some interface for postgres database management, you can install pgAdmin on your local computer and connect to the remote machine using its IP and the postgres exposed port (by default 5432).
docker ps -a to get container ids then
docker exec -it psql -U -W

docker container can not connect to local postgres [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 2 years ago.
Can't connect to Postgres from the docker container.
I do not want user docker-compose and create a Postgres container, already got Postgres app running. I think it is a bad idea to container postgres and better use system.
OSX, Postgres10, Flask
I already made
postgresql.conf
listen_addresses = '*'
port = 5432
pg_hba.conf
host all all 0.0.0.0/0 trust
I Used trust for any result, but no effect.
Dockerfile
FROM python:3.8-alpine
RUN apk update \
&& apk add --virtual build-deps gcc musl-dev \
&& apk add python3-dev \
&& apk add postgresql-dev \
&& apk add jpeg-dev zlib-dev libjpeg \
&& pip install --upgrade pip
ENV PYTHONUNBUFFERED 1 \
&& FLASK_APP app.py
#EXPOSE 5000 5432
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
CMD ["./bin/run.sh"]
./bin/run.sh
#!/bin/sh
#python run.py
source venv/bin/activate
set -e
flask db upgrade
exec gunicorn -b --workers 4 --access-logfile --error-logfile run:app :5000
The "docker run" command I try to use:
docker run --rm -e SQLALCHEMY_DATABASE_URI=postgresql://postgres:postgres#0.0.0.0:5432/my_db --net=host -p 5000:5000 my_container:v0.1
the command leads to error
...
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
command connect me to Postgres
psql -U postgres -h 0.0.0.0
Trying to connect to host "0.0.0.0" is technically wrong. (the address 0.0.0.0 is used to other purposes no to connect to...)
Did you tried connect using the postgres container ip address instead of "0.0.0.0"?
You can get the postgres container ip address throught the following command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_of_postgres_container

Docker on MacOS, unable to bind Postgres to port 5432

Unsure if this is an issue with Docker for Mac, my Postgres installation (via Homebrew, or some other strange local issue going on.
Probably worth mentioning that I have never ran into this issue on Linux.
Here is a snippet from my docker-compose.yml file that uses the postgres image:
services:
postgres:
image: postgres
environment:
- POSTGRES_DB=some_db
- POSTGRES_USER=some_user
- POSTGRES_PASSWORD=supersecretpassword
ports:
- 5432:5432
After starting compose services docker-compose up, I can see my postgres container running (docker ps).
I can also connect to it directly via docker exec
docker exec -it <container_id> psql -d some_db -U some_user
Everything working as expected so far...
However, when I try to connect to my Docker postgres instance via my local psql client:
psql -h localhost -p 5432 -U some_user -d some_db
It is not connecting to the Docker instance of postgres and rather is trying to use my local postgres instance.
Now the strange part...
When I change the port binding in docker-compose.yml
from 5432:5432
to 5433:5432 (5433 can be any open port)
I am able to connect to the Docker postgres instance as expected via my local psql client:
psql -h localhost -p 5433 -U some_user -d some_db
# After being prompted for my password I'm in!
I don't mind binding to a different port, but I'm still so curious what's happening here!
Anyone know what is happening?
Am I just not able to bind to port 5432?
Is this a Docker for Mac thing?
Is there an issue in the way I installed Postgres?
Thanks!

connect to postgres container from flask app inside container

I have my flask app app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
APP = Flask(__name__)
DB = SQLAlchemy()
if __name__ == '__main__':
APP.config.from_mapping(
SQLALCHEMY_DATABASE_URI='postgres://postgres:password#0.0.0.0:5432',
SQLALCHEMY_TRACK_MODIFICATIONS=False
)
DB.init_app(APP)
DB.create_all(app=APP)
APP.run(use_reloader=False, host='0.0.0.0', port='5000')
and I have a Dockerfile for it:
FROM python:3.6-alpine
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
WORKDIR /root
COPY app.py .
RUN pip3 install Flask==1.0.2
RUN pip3 install psycopg2-binary==2.7.6.1
RUN pip3 install Flask-SQLAlchemy==2.3.2
CMD ["python3", "app.py"]
I run:
docker build . --tag flaskapp:1
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=password --name database postgres
docker run --rm -p 5000:5000 flaskapp:1
I then get an exception which points out:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
How do I fix this?
You have specified 0.0.0.0 as the IP address to connect to, which doesn't make sense. 0.0.0.0 is the "Any Address". You probably saw a message that postgres was listening on 0.0.0.0, which is where you got it from. In the context of a server listening on 0.0.0.0, it means that it is listening on all ipv4 interfaces. See https://en.wikipedia.org/wiki/0.0.0.0 for more information about the special 0.0.0.0 address and what it means.
If you want to connect to the postgres service, then you would need to use a valid ip address or dns name of where it is running.
In Docker, if you have multiple named containers connected to the same user-defined network, you can make use of the built-in service discovery mechanism that Docker ships with.
Here's a modified set of commands to run to take advantage of this:
docker network create mynet
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=password --net mynet --name database postgres
docker run --rm -p 5000:5000 --net mynet flaskapp:1
Be sure to change your code to connect to postgres://postgres:password#database:5432 instead of postgres://postgres:password#0.0.0.0:5432

Docker container for Postgres 9.1 not exposing port 5432 to host

I'm trying to use a Docker container to run a PostgreSQL server, and connect with it from my host machine.
My configuration is:
Host machine: Mac OS X 10.10.5
Docker 1.10.1
I've done this:
Step 1: create a volume for permanent postgres data
docker volume create --name postgres_data
Step 2: Start the postgres instance
UPDATE: As suggested in comments, I specified port mapping when running the container
docker run --name my_postgres_container -e POSTGRES_PASSWORD=my_password -v postgres_data:/var/lib/postgresql/data -p 5432:5432 -d postgres:9.1
Step 3: connect to Docker instance by doing this:
docker run -it --link my_postgres_container:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
But I want to connect to that instance just by:
psql -h localhost -p 5432 -U postgres
Like if I had installed Postgres locally in my host machine.
The problem is port 5432 is not exposed. So, I can't connect with it:
sudo lsof -i -P | grep -i "listen" --> no port 5432 open
UPDATE 2: Even stranger. I've also done this:
Stop Docker. Then, run a normal PostgreSQL 9.4.4 instance in my host machine (no docker involved here, just postgres running in my Mac OS X host, listening on port 5432). Everything is normal:
sudo lsof -i -P | grep -i "postgres"
postgres 14100 jorge 6u IPv4 0x780274158cebef01 0t0 TCP localhost:5432 (LISTEN)
I can connect with my local postgres instance without any problem (look the output of the command: is the postgres compiled for Mac OS X, my host):
psql -h localhost -U postgres -c "select version()"
version
---------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 9.4.4 on x86_64-apple-darwin14.3.0, compiled by Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), 64-bit
(1 row)
Now the fun part. I start my Docker instance again, while the host PostgreSQL instance is running.
It starts! (and it shouldn't). I can even connect using docker run...
docker run -it --link my_postgres_instance:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
If I run select version() now, it shows postgres running inside my docker instance at the same time postgres is running in my host, out of docker, using the same 5432 port. (Look at the ouput, is postgres compiled for Debian, the OS inside the postgres:9.1 container)
postgres=# select version();
version
------------------------------------------------------------------------------------------------
PostgreSQL 9.1.20 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
Why?
Does it make sense? My final goal is to run a Django app in another Docker container and connect with my Postgres instance. How could I do that?
It's 2018 and I just had a similar problem. The solution for me seemed to be with the order of props to docker. e.g. this resulted in no port being exposed;
docker run -d --name posttest postgres:alpine -e POSTGRES_PASSWORD=fred -p 5432:5432
while this worked fine (image exposed port 5432 as expected);
docker run --name posttest -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine
Run the postgre image with the correct Port Mapping using -p <host_port>:<container_port>:
docker run --same-options-as-step-one -d -p 5432:5432 postgres:9.1
Your docker host is a virtual machine, which has it's own IP adddres.
You can detect this IP address by entering the following command:
docker-machine ip
The answer will be something like 192.168.99.100
When you have mapped the ports using the -p 5432:5432 switch, you will be able to connect to postgres with any tool from your dev machine using the IP address mentioned.
I was able to connect using container IP or host IP, except localhost (127.0.0.1).
To get container id run
docker ps
Find required container id and run
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>
Port must be exposed.
Here is an example of docker-compose.yml which starts two containers postgres and adminer, which is database management tool you can use to connect to postgres:
version: '3'
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
postgres:
image: postgres:11.4-alpine
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
I had a similar issue. My problem was simply 0.0.0.0 not mapping to localhost so I just had to add to psql
psql --host=0.0.0.0
This is presuming
docker port <container name>
outputs
5432/tcp -> 0.0.0.0:5432
Other answers work, but don't explain why they work.
Given the command:
psql -h localhost -p 5432:5432 -U postgres
localhost is actually a special value that tells psql to look for a unix socket connection, instead of going over TCP. We can't use unix sockets to connect to docker services.
Changing the command like so fixes it, by forcing TCP:
psql -h 127.0.0.1 -p 5432:5432 -U postgres
That will work as long as you docker run ... -p 5432:5432 .... Specifying the IP returned by docker-machine ip also forces TCP, so that also works.
I had a similar problem working in a VMWare virtual machine with Lubuntu. The VM had been paused and then was restarted. The PostgreSQL Docker container was correctly mapped and listening on localhost:5432, but I always got:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Restarting the VM solved the problem in my case.
Try this to install postgresql
docker run --name postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine
Or Change port host machine Heree (mac)
docker run --name postgres -d -p 5436:5432 -e POSTGRES_PASSWORD=fred postgres:alpine
Tip:
Install pgadmin4
docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=name#example.com" -e "PGADMIN_DEFAULT_PASSWORD=admin" -d dpage/pgadmin4