Can not connect docker containered postgres db - postgresql

docker-compose.yml:
services:
db:
image: mdillon/postgis:11
restart: always
ports:
- "5465:5432"
volumes:
- ./data/postgisdb:/var/lib/postgresql/data
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=p#ssword
- POSTGRES_DB=db_test
Then I run docker-compose up -d --build --remove-orphans
docker ps shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7d734c1f23c mdillon/postgis:11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5465->5432/tcp, :::5465->5432/tcp app_db_1
When I try to connect database via pgadmin with parameters
Host name/address: localhost
Port: 5465
Maintence database: db_test
Username: user
Password: p#ssword
I still get error password authentication failed for user "user".
What am I doing wrong?
Inside container env shows:
LANG=en_US.utf8
HOSTNAME=bbc96a3254ed
PG_MAJOR=11
PWD=/
HOME=/root
PG_VERSION=11.2-1.pgdg90+1
GOSU_VERSION=1.11
PGDATA=/var/lib/postgresql/data
POSTGRES_DB=db_test
POSTGIS_MAJOR=2.5
POSTGRES_PASSWORD=p#ssword
POSTGRES_USER=user
SHLVL=1
POSTGIS_VERSION=2.5.2+dfsg-1~exp1.pgdg90+1
...

Related

problem with postgres docker container inside Gitlab CI

It's been few days I am blocked on this problem with my project, it's working on localhost but not on gitlabCI.
I would like to build a test database on the postgres docker image in gitlabCI but it doesn't work, I have try a lot of things and lose a lot of hours before ask this there :'(.
below my docker-compose.yml file :
version: "3"
services:
nginx:
image: nginx:latest
container_name: nginx
depends_on:
- postgres
- monapp
volumes:
- ./nginx-conf:/etc/nginx/conf.d
- ./util/certificates/certs:/etc/nginx/certs/localhost.crt
- ./util/certificates/private:/etc/nginx/certs/localhost.key
ports:
- 81:80
- 444:443
networks:
- monreseau
monapp:
image: monimage
container_name: monapp
depends_on:
- postgres
ports:
- "3000:3000"
networks:
- monreseau
command: "npm run local"
postgres:
image: postgres:9.6
container_name: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_HOST: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./pgDatas:/var/lib/postgresql/data/
- ./db_dumps:/home/dumps/
ports:
- "5432:5432"
networks:
- monreseau
networks:
monreseau:
and below my gitlab-ci.yml file:
stages:
# - build
- test
image:
name: docker/compose:latest
services:
- docker:dind
before_script:
- docker version
- docker-compose version
variables:
DOCKER_HOST: tcp://docker:2375/
# build:
# stage: build
# script:
# - docker build -t monimage .
# - docker-compose up -d
test:
stage: test
script :
- docker build -t monimage .
- docker-compose up -d
- docker ps
- docker exec -i postgres psql -U postgres -h postgres -f /home/dumps/test/dump_test_001 -c \\q
- exit
- docker exec -i monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js
- exit
this is the content of docker ps log on gitlabCI server :
docker ps on gitlab-CI
I thought to put postgres on host would work, but no I always have in gitlab-ci terminal:
psql: could not connect to server: Connection refused
Is the server running on host "postgres" (172.19.0.2) and accepting
TCP/IP connections on port 5432?
I also tried to put docker on host but error :
psql: could not translate host name "docker" to address: Name or service not known
little precision : it is working on localhost of my computer when i am doing make builded-test
bellow my makefile:
builded-test:
docker build -t monimage .
docker-compose up -d
docker ps
docker exec -i postgres psql -U postgres -h postgres -f /home/dumps/test/dump_test_001 -c \\q
exit
docker exec -i monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js
exit
docker-compose down
I want to make work postgres image in my docker-compose on gitlab CI to execute my tests help me please :) thanks by advance
UPDATE
Now it working in gitlab-runner but still not on gitlab when I push, I update the files like following
I added :
variables:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ""
POSTGRES_HOST_AUTH_METHOD: trust
and changed
test:
stage: test
script :
- docker build -t monimage .
- docker-compose up -d
- docker ps
- docker exec postgres psql -U postgres **-h postgres** -f /home/dumps/test/dump_test_001
- docker exec monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js
in the .gitlab-ci.yml
but still don't work when I push it to gitlab, it give me :
sql: could not connect to server: Connection refused
Is the server running on host "postgres" (172.19.0.2) and accepting
TCP/IP connections on port 5432?
any ideas ? :)
Maybe you need to wait for PostgreSQL service to be up and running.
Can you add a 10 seconds delay before trying the psql stuff? Something like:
- sleep 10
If it works, then you can use a more specific solution to wait for PostgreSQL to be initialized, like Docker wait for postgresql to be running

Pgadmin4 cannot connect to my postgres database

here is my docker-compose.yml file:
version: "3.5"
services:
db:
image: myapp
container_name: my-database
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypasswd
POSTGRES_DB: mydb
expose:
- "5432"
ports:
- 8000:5432
volumes:
- /path/to/my/migrationsV1_0__audit_table.sql
- //path/to/my/migrations/V1_1__tables.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Then i $ docker inspect xxxxxxxx | grep "IPAddress"
It produces output:
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
After docker-compose I enter values to pgadmin as so:
Pgadmin_capture
Yet pgadmin gives error:
Unable to connect to server
timeout expired.
What is wrong here?
In your configuration PostgreSQL database has 5432 port (it is default value).
Are you sure in this line?
PGADMIN_LISTEN_PORT=5454
Try to replace it with
PGADMIN_LISTEN_PORT=5432
for a workaround to this issue, just run the container with --user=root (in docker-compose use user: root)
You need to use the gateway address of Postgres container. Use docker inspect xxxxxxxx | grep Gateway to get it. I guess in your case it would be 172.17.0.1
In my case, my pgadmin container didn't shared the DB container's network.
So, you should run this command.
docker network inspect NETWORK <YOUR DB CONTAINER'S NETWORK>
And if it doesn't, run this command
docker network connect <YOUR_DB_CONTAINER'S_NETWORK> <YOUR_PGADMIN_CONTAINER'S NAME>

cannot access postgres running in docker via pg_isready or pgAdmin

I have a simple postgres running in docker. Everything works ok. I also can access it via:
docker exec -it db bash and there using psql.
But both pg_isready and pgAdmin fail to access it via localhost at port 5432
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
networks:
frontend:
aliases:
- db
That's how the container status look like:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9295d519be2s postgres "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:5432->5432/tcp ebot_db_1
What is wrong?

Docker-compose postgresql password authentication failed

I'm trying to get a setup going with a webservice that consumes a postgres database. Should be simple to setup, but I'm getting errors. So, first thing I want to make sure is that the database I set up is actually there and running.
To test this I substitute the "consumer" or "client" for an alpine interactive shell like so:
version: '3'
services:
db:
image: postgres:10.1-alpine
container_name: db
expose:
- 5432
volumes:
- "dbdata:/var/lib/postgresql/data"
environment:
- POSTGES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
web:
image: alpine:latest
stdin_open: true
tty: true
entrypoint: /bin/sh
depends_on:
- db
volumes:
dbdata:
Then I run the following command to get into the interactive shell:
docker-compose run web
and the following command to get in the database:
apk --update add postgresql-client && rm -rf /var/cache/apk/*
psql -h db -U user db
I get a plain denial from postgresql:
psql: FATAL: password authentication failed for user "user"
Same error message for each combo of username/password/databasename I try. Not much helpful.
What am I doing wrong here?
You have a typo in your docker-compose file. You mispelled POSTGRES here:
POSTGES_USER=user
That means the user user isn't being created. If I correct that typo, so that I have:
version: '3'
services:
db:
image: postgres:10.1-alpine
expose:
- 5432
volumes:
- "dbdata:/var/lib/postgresql/data"
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
web:
image: alpine:latest
stdin_open: true
tty: true
entrypoint: /bin/sh
depends_on:
- db
volumes:
dbdata:
Start the environment:
docker-compose up -d
Attach to the web contained and install the postgresql client:
$ docker attach project_web_1
/ # apk add --update postgresql-client
Then I can connect without a problem:
/ # psql -h db -U user db
Password for user user:
psql (11.2, server 10.1)
Type "help" for help.
db=#

Can not connect to Postgres Container from pgAdmin

related posts:
1) docker postgres pgadmin local connection
2) https://coderwall.com/p/qsr3yq/postgresql-with-docker-on-os-x (in the example "Name" entry is not filled in)
there are two ways to complete this task, I use official postgres
METHOD 1:
and runs it with
sudo docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
then connect with
Name: postgres
Host: localhost
Port: 5432
user
pass
...
METHOD 2:
starts with
sudo docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
and then check the ip of container
sudo docker inspect
say result
172.17.42.1
then connect with pgAdmin tab Properties filled info
Name: postgres
Host: 172.17.42.1
Port: 5432
user
pass
...
I included this in the docker yaml file to get the database and pgAdmin:
database:
image: postgres:10.4-alpine
container_name: kafka-nodejs-example-database
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
expose:
- "5432"
ports:
- 8000:5432
volumes:
- ./services/database/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./services/database/seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
The postgres username is alphaone and the password is xxxxxxxxxxx.
Do a docker ps to get the container id and then docker inspect <dockerContainerId> | grep IPAddress
eg: docker inspect 2f50fabe8a87 | grep IPAddress
Insert the Ip address into pgAdmin and the database credentials used in docker:
Since you're mapping the port 5432 on the container to the same port on host with -p 5432:5432 in your docker run statement, try connecting pgadmin to port 5432 on the host instead of the container.