Cannot connect to postgres running in Docker - postgresql

I am on a Mac.
I pulled the postgres docker image by running:
docker pull postgres:12.4
Then I start the container by running:
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=pass -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
I can confirm the container is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ef29362b6f3 postgres "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:5432->5432/tcp pg-docker
I installed only psql on the host and when I try to connect to the postgres, I get the following error:
psql -h localhost -U postgres -d postgres
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
password retrieved from file "/Users/me/.pgpass"
I am actually following the steps as outline in this post
I am not sure what is going wrong or how to fix this. Any ideas?

Related

cannot authenticate with postgres

I'm trying to connect to running postgres container with psql:
docker pull postgres
docker run -e POSTGRES_PASSWORD=password -d postgres
C:\Program Files\PostgreSQL\13\bin>psql <myUserName>
Password for user <myUserName>:
at this point I type the given password, in this case just password and get the error
psql: error: FATAL: password authentication failed for user "<myUserName>"
What am I doing incorrectly ?
When you use docker run -e POSTGRES_PASSWORD=password -d postgres, the POSTGRES_PASSWORD would be set for user postgres as default. you can specify your user with POSTGRES_USER environment.
Second thing is that when you run a postgresql container and don't bind any ports for that, you can't connect to that container from outside. So you won't being able to connect to your container with pure pqsl command. Here you have 3 way to connect to your container:
1- Use docker run -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres to run container, then connect to it with psql -h <YOUR_IP> -p 5432 -U <USERNAME>
2- Get your container ip with docker inspect <CONTAINER_NAME> command, then connect to it with psql -h <CONTAINER_IP> -U <USERNAME>
3- Use psql inside your container with docker exec -it <CONTAINER_NAME> psql -U <USERNAME>

Access Postgres database container but get error

I use docker to run a Postgres database container. My command to start it is:
docker run --name postgres-me -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:alpine
Then it is up and running:
~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b662971d9b54 postgres:alpine "docker-entrypoint.s…" 6 seconds ago Up 3 seconds 0.0.0.0:5432->5432/tcp postgres-me
Then I try to access the container by:
~ $ docker exec -it b662971d9b54 bin/bash
bash-5.0# psql
psql: error: could not connect to server: FATAL: role "root" does not exist
As you can see above, after I go into the container, then I tried command psql, but I got error could not connect to server: FATAL: role "root" does not exist
How to get rid of this error?
You need to provide username as well.
Try this :
psql -U <username>

Docker Postgres - error while creating a database and user

I use the official Postgres image from the Docker Hub
docker pull postgres
I start my container in my machine on local:
docker run --name some-postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=test postgres
The container would have to create my test base with my user on port 542
I have this error when I want to connect on my db
$ sudo docker run -it postgres /bin/bash
root#ef4407c26a96:/# su postgres
postgres#ef4407c26a96:/$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
postgres#ef4407c26a96:/$
Please provide the database that you want to connect to. As you've provided POSTGRES_DB=test, use it when you wish to connect to psql:
psql -d test -U user
Also, I was able to connect with psql both as root and postgres user.

How can I connect to Postgres database in the container via port 5432

I am running a postgres docker container by using the commands below: (reference: https://docs.docker.com/engine/examples/postgresql_service/)
docker build -t eg_postgresql .
docker run --rm -P --name pg_test eg_postgresql
This works but the port number is dynamic. I can connect to the database by giving the port number. (the port I see in docker ps command)
I would like to connect to this docker database from Python so I need a static port number.
I tried the parameters below:
-p 127.0.0.1:5432:5432
-p 5432:5432
In that case, the docker container's port number was set as 5432. However, I could not connect to the database. I get docker user does not exist error message.
What is your advice?
I took the Dockerfile from the link you posted. After building the container with
docker build -t eg_postgresql .
I started the container with
docker run --rm -p 5432:5432 --name pg_test eg_postgresql (which binds localhost port 5432 to the container port 5432)
and then I tried to connect with
psql -h localhost -p 5432 -d docker -U docker --password
It works like a charm. If you get a message that docker user does not exist please double check that all steps from the Dockerfile are executed succesfully during the docker build command as the creation of the docker user is done in the command RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -O docker docker. Make also sure that you have no PostgreSQL server running on your localhost so that you can be sure that you are trying to connect to PostgreSQL inside the container.

Problem with postgresql and pgadmin docker containers

I'm trying to connect postgresql and pgadmin4 work together.
pgadmin4 works fine but when I try to create a new server I have 2 problems:
if the postgres container is at other port that is not 5432 it dont recognize that port. It show this error: could not connect to server: Connection refused
Is the server running on host "172.17.0.5" and accepting
TCP/IP connections on port 5431?
if the postgres container is at port 5432 the error is FATAL: password authentication failed for user "example".
I execute this command to get postgres container: docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres.
I try, following other responses in stackoverflow, adding this command -c"listen_addresses='*'" and I enter in the config file too but noone of this work to me.
Hope you can help me, thanks.
EDIT [Solved]
Ok I solved, it was a big fail by my part. I was using 172.17.0.5 (the IP container address) and what I need to use to connect is 172.17.01 (the Gateway).
Thanks for you time.
I have reproduce your scenario this way:
# docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4030c577a24 postgres "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5431->5432/tcp postgres2
# sudo -u postgres psql -h localhost -p 5431
could not change directory to "/root": Permission denied
Password:
psql (10.5, server 11.2 (Debian 11.2-1.pgdg90+1))
WARNING: psql major version 10, server major version 11.
Some psql features might not work.
Type "help" for help.
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# \q
Now starting docker for pgadmin and being able to connect to postgresql:
docker run -p 80:80 --link postgres2 -e "PGADMIN_DEFAULT_EMAIL=user#domain.com" -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" -d dpage/pgadmin4
With the above command you can link the postgres2 docker to the pgadmin docker and then on creating a connection on pgadmin4 you should use:
host name/address: postgres2
port: 5432
Maintenance database: postgres
username: postgres
with that, I've connected to Postgres from pgadmin4
As far as I know, docker PostgreSQL comes by default with localhost only connection and if you want to add remote connection you should add "listen_addresses = '*'" to postgresql.conf