Error while running Postgres container with docker-compose - postgresql

After running docker-compose up. I get the error below
FATAL: password authentication failed for user "postgres"
DETAIL: Connection matched pg_hba.conf line 95: "host all all all md5"
Waiting for db
Here is my docker-compose.yml file
version: '3'
services:
redis:
image: redis
db:
image: postgres:9.4
environment:
- POSTGRES_USER:postgres
- POSTGRES_PASSWORD:postgres
vote:
image: voting-app
ports:
- 5000:80
worker:
image: worker-app
result:
image: result-app
ports:
- 5001:80

I used postgres image version 12-alpine and it somehow worked
image: postgres:12-alpine
environment:
- POSTGRES_USER = postgres
- POSTGRES_PASSWORD = postgres

Related

Connect to Postgres in DataGrip

I try to connect to my Postgres database built by docker in DataGrip but I get connection error.
Here is my application.yml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:postgresql://db:5432/postgis_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: postgres
password: postgres
Here is docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
restart: always
volumes:
- db:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgis_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- app-network
geolocation-service:
image: geolocation-service
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- "8080:8080"
networks:
- app-network
volumes:
db:
networks:
app-network:
DataGrip connection settings:
And the error I get:
Does anybody knows how to solve this?
Connecting to this db by shell works fine:
docker exec -it a37 psql -U postgres postgis_db
Please check pg_hba.conf file, you have to allow connections from all hosts for this user, by default it is restricted to localhost.

psycopg2.OperationalError: FATAL: the database system is starting up, Docker + Odoo

i have this docker-compose file:
version: '3.7'
services:
db:
image: "postgres:9.6"
container_name: postgres-container
ports: ["6543:5432"]
environment:
- POSTGRES_USER=odoo
- POSTGRES_PASSWORD=admin
- POSTGRES_DB=odoo
restart: always
volumes:
- ./data/postgres:/var/lib/postgresql/data
odoo:
#build: ./odoo-container
image: odoo-image
container_name: odoo-container
ports: ["8069:8069"]
tty: true
command: opt/odoo/odoo-bin -c opt/odoo.conf -d teste
depends_on:
- db
the problem is that when i start docker compose, my db service runs and when docker runs the odoo service i get an error:
psycopg2.OperationalError: FATAL: the database system is starting up
and when i restart the odoo container, its works
im added the restart method, and works:
odoo:
#build: ./odoo-container
image: odoo-image
container_name: odoo-container
ports: ["8069:8069"]
command: opt/odoo/odoo-bin -c opt/odoo.conf -d teste
depends_on:
- db
restart: always

Problem connecting to postgresql hosted in docker

I have below error connecting to postgresql database hosted in docker.
myapp_Container | WARNING - ConnectionBus: Database connections: 0 active, 0 idle.
myapp_Container | ERROR - ConnectionBus: Opening JDBC connection to Some(db:54325) failed with SQLState: 08001 Error code: 0 Message: The connection attempt failed., giving up...(4/4)
myapp_Container | WARNING - ConnectionBus: Cannot create database connection.
I have two docker-compose files as below
APP Docker-Compose file
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
network_mode: postgres_net
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db:54325/mydb
external_links:
- db:localpostgres_1
DB Docker-Compose file
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
I created postgres_net via docker network create -d bridge postgres_net
Why am I doing this? I want to create apps with same docker-compose structure without creating a postgresql db for each, if I put them together like below it will work.
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db/mydb
links:
- db
db:
image: postgres
container_name: postgres_x
ports:
- "54320:5432"
environment:
- TZ=Europe/Amsterdam
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
restart: always
I can connect to the Db via pgadmin, but can't understand why myapp cannot connect, please help
pg_hba.conf
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all all md5
You have not properly defined network in First compose file(App compose). You have specified postgres_net as network_mode: postgres_net which is invalid. You need to specify it properly like in the second compose file.
Corrected compose file will be:
APP Docker-Compose
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db:54325/mydb
networks: # declare network here
- postgres_net
networks: #define network
postgres_net:
external: true
DB Docker-Compose
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
this config worked perfectly
app docker-compose file
version: "3"
services:
mendix:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
network_mode: postgres_net
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#localpostgres_1/myapp
external_links:
- localpostgres_1
networks:
postgres_net:
external: true
db docker-compose
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
as probably people may know, one may run the docker-compose files in a different folder per application, because docker-compose cares about the path and overwrites the last container you created in a given folder.

I cannot connect from adminer to postgresql

I try to up postgresql and adminer via docker container. But from adminer I cannot enter to postgresql with password and user I whote.
SQLSTATE[08006] [7] FATAL: password authentication failed for user "root"
I tried all.
version: '3'
services:
web:
build: .
environment:
- APACHE_RUN_USER=www-data
volumes:
- ./blog:/var/www/html/
ports:
- 8080:80
working_dir: /var/www/html/
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: kisphp
POSTGRES_USER: root
POSTGRES_DB: kisphp
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
adminer:
image: adminer
restart: always
ports:
- "6080:8080"
This docker-compose configuration works well.
Try recreating it from scratch:
Delete ./postgres folder
docker-compose stop
docker-compose down
docker-compose up -d

Connecting pgadmin to postgres in docker

I have a docker-compose file with services for python, nginx, postgres and pgadmin:
services:
postgres:
image: postgres:9.6
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5431:5431"
pgadmin:
image: dpage/pgadmin4
links:
- postgres
depends_on:
- postgres
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: pwdpwd
volumes:
- pgadmin:/root/.pgadmin
ports:
- "5050:80"
backend:
build:
context: ./foobar # This refs a Dockerfile with Python and Django requirements
command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
volumes:
- staticfiles_root:/foobar/static
depends_on:
- postgres
nginx:
build:
context: ./foobar/docker/nginx
volumes:
- staticfiles_root:/foobar/static
depends_on:
- backend
ports:
- "0.0.0.0:80:80"
volumes:
postgres_data:
staticfiles_root:
pgadmin:
When I run docker-compose up and visit localhost:5050, I see the pgadmin interface. When I try to create a new server there, with localhost or 0.0.0.0 as host name and 5431 as port, I get an error "Could not connect to server". If I remove these and instead enter postgres in the "Service" field, I get the error "definition of service "postgres" not found". How can I connect to the database with pgadmin?
the docker container name changes when you run docker-compose to prefix the folder name (to keep container names unique). You could force the name of the container with container_name property
version: "3"
services:
# postgres database
postgres:
image: postgres:12.3
container_name: postgres
environment:
- POSTGRES_DB=admin
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_HOST_AUTH_METHOD=trust # allow all connections without a password. This is *not* recommended for prod
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
ports:
- "5432:5432"
# pgadmin for managing postgis db (runs at localhost:5050)
# To add the above postgres server to pgadmin, use hostname as defined by docker: 'postgres'
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=admin
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=5050
ports:
- "5050:5050"
volumes:
database-data:
Another option is to connect the postgres container to localhost with
network_mode: host
But you lose the nice network isolation from docker that way
Be careful that the default postgres port is 5432 not 5431. You should update the port mapping for the postgres service in your compose file. The wrong port might be the reason for the issues you reported. Change the port mapping and then try to connect to postgres:5432. localhost:5432 will not work.