I try to connect my spring boot application and PostgreSQL which is in a docker container.
When I launch my docker-compose, docker create a container and database with the user I set it in this file.
every thing is ok until here.
But when I try to connect my spring application and this container I get this error :
org.postgresql.util.PSQLException: FATAL: authentification par mot de passe échouée pour l'utilisateur « postgres » (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf)
The message is in French, In general it can't connect to my database with that user postgres.
My docker-compose.yml
version: '3.5'
services:
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
POSTGRES_DB: ${POSTGRES_DB:-dbname}
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
and my application.property
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
any one have an idea about this issue please?
I'm not sure but I assume it's an issue with the defined network. As per default standalone containers use the default network / bridge from docker which offers basic networking functionality. But since you defined a custom network, I assume your spring-app which is running on your host system is not able to reach your container.
Try to remove the networks part and try it.
Related
I have created a docker image with this command docker compose up -d
where I was able to load pgAdmin instance in http://localhost:5050/browser/
create a database and table in the same , credentials are working properly.
However when I start to run my main spring boot application CustomerApplication it fails with below error >
org.postgresql.util.PSQLException: FATAL: la autentificación password falló para el usuario «amigoscode» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf)
I do not know what is wrong, my credentials are correct.
what seems to be the issue?
below are application.yml and docker-compose.yml
docker-compose.yml
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: amigoscode
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
application.yml
server:
port: 8080
spring:
application:
name: customer
datasource:
username: amigoscode
url: jdbc:postgresql://localhost:5432/customer
password: password
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
customer table Script
CREATE DATABASE customer
WITH
OWNER = amigoscode
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
Since I have a postgres instance installed and running in my local (port 5432), the microservice customer was trying to connect to that instance, not the one from docker which was using the same port.
the solution was to change the url port from application.yml
url: jdbc:postgresql://localhost:5433/customer
and the port of postgres from docker-compose.yml
from
ports:
- "5432:5432"
to
ports:
- "5433:5432"
so microservice connects to the postgres instance in the docker image, not the local one
then re-run docker command docker compose up -d
run the CustomerApplication (SprinbgootApplication) and this time
application starts up nice and smoothly by creating the customer table.
When you created the role amigoscode did you actually set a password for it?
CREATE ROLE amigoscode WITH LOGIN PASSWORD 'password';
You may have logged into pgadmin as the postgres superuser, created the role amigoscode and never set a password. The encoding issue looks similar to the one specified here which appears when the supplied password is incorrect. It looks like that pgjdbc bug was fixed in 2014, but has possibly regressed in more modern versions >= 42.2.x as there is currently a similar unresolved issue.
Other likely alternatives:
SPRING_DATASOURCE_PASSWORD environment variable is set to something else.
You also have an application.properties file which has a different password that will take precedence over the one in application.yml.
You have a different password specified in any of the property sources that spring checks before application.yml (discussion of precedence here)
The same happened to me, the problem is that i was running another instance that was using the port 5432 (postgresql server and docker), I was using dbeaver and it was trying to connect to that instance instead the one that i wanted to use.
Check which applications are using the port and close the ones that you don't need and try again to connect to the database.
I have a docker-compose that brings up the psql database as below, currently I'm trying to connect to it with pgAdmin4 (not in a docker container) and be able to view it. I've been having trouble authenticating with the DB and I don't understand why.
docker-compose
version: "3"
services:
# nginx and server also have an override, but not important for this q.
nginx:
ports:
- 1234:80
- 1235:443
server:
build: ./server
ports:
- 3001:3001 # app server port
- 9230:9230 # debugging port
env_file: .env
command: yarn dev
volumes:
# Mirror local code but not node_modules
- /server/node_modules/
- ./server:/server
database:
container_name: column-db
image: 'postgres:latest'
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres # The PostgreSQL user (useful to connect to the database)
POSTGRES_PASSWORD: root # The PostgreSQL password (useful to connect to the database)
POSTGRES_DB: postgres # The PostgreSQL default database (automatically created at first launch)
volumes:
- ./db-data/:/var/lib/postgresql/data/
networks:
app-network:
driver: bridge
I do docker-compose up then check the logs, and it says that it is ready for connections. I go to pgAdmin and enter the following:
where password is root. I then get this error:
FATAL: password authentication failed for user "postgres"
I check the docker logs and I see
DETAIL: Role "postgres" does not exist.
I'm not sure what I'm doing wrong, according to the docs the super user should be created with those specifications. Am I missing something? Been banging my head against this for an hour now. Any help is appreciated!
#jjanes solved it in a comment, I had used a mapped volume and never properly set up the db. Removed the volume and we're good to go.
Docker container for cvat_db has following settings:
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
environment:
POSTGRES_USER: root
POSTGRES_DB: cvat
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
cvat_db:/var/lib/postgresql/data
networks:
cvat
While below is the connection setting in dbeaver, where "HOST IP" i have put the IP address where cvat is hosted.
Dbeaver Settings
I'm getting error of timeout connection. So, I want to know how to connect postgres database to dbeaver.
Keep the following in mind:
Postgres always need a password according to their docs.
Do not create custom networks if it is not really needed. Use the default bridge network instead.
Do you connect with Postgres from another docker container or from your host system? If you connect from your host system add ports with 5432:5432.
mount your volumes to a subpath instead of named volumes
Example compose file:
version: '3.9'
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: cvat
volumes:
- ./cvat_db:/var/lib/postgresql/data
I wrote an article about docker compose networking, perhaps it helps.
I have used docker-compose.yaml. I have configured in docker-compose.yml bellow like:
Step 1: In docker-compose.yaml I user bellow code
services:
postgres:
image: postgres
container_name: postgres
hostname: postgres
volumes:
- db-data:/var/lib/postgresql/data
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: accountdb
account-opening:
image: ehaque95/pocv1-account-opening:0.0.1-SNAPSHOT
mem_limit: 700m
ports:
- "8081:8081"
networks:
- account-network
depends_on:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/accountdb
volumes:
db-data:
Step 2 :
I have configure applicaiton.yml in spring boot bellow like:
spring:
datasource:
url: jdbc:postgresql://postgres:5432/accountdb
username: postgres
password: postgres
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
database: postgresql
hibernate:
ddl-auto: update
properties:
dialect: org.hibernate.dialect.PostgreSQLDialect
Note : postgresql server IP change frequently. When i run docker inpspect ,it shows some "IPAddress": "172.19.0.2" or sometimes "IPAddress": "172.19.0.3".It shows when I run docker-compose up again.
It shows connection error.what is the wrong of my code connect to postgresql. Please help me
For two containers to communicate with each other, they must be on the same Docker network. By default, Compose creates a network named default and attaches containers to it; if you specify other networks: for a container, then they are not attached to the default network.
In your docker-compose.yml, the account-opening container has a networks: block, but postgres doesn't:
version: '3.8'
services:
postgres:
...
# no networks:, so implicitly
# networks: [default]
account-opening:
...
networks: [account-network] # and not default
# The postgres container is on the default network
# This container is on account-network
# And so this host name doesn't resolve
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/accountdb
There's nothing wrong with using the default network. For most typical applications you can delete all of the networks: blocks in the entire file. Then the default network will get created with default settings, and all of the containers will attach to that network, and be able to address each other by their Compose service name.
try
url: jdbc:postgresql://localhost:5432/accountopendb
if you are running both the spring app and the postgresql container in the same machine in your datasource config.
I have a PostgreSQL container set up that I can successfully connect to with Adminer but I'm getting an authentication error when trying to connect via something like DBeaver using the same credentials.
I have tried exposing port 5432 in the Dockerfile and can see on Windows for docker the port being correctly binded. I'm guessing that because it is an authentication error that the issue isn't that the server can not be seen but with the username or password?
Docker Compose file and Dockerfile look like this.
version: "3.7"
services:
db:
build: ./postgresql
image: postgresql
container_name: postgresql
restart: always
environment:
- POSTGRES_DB=trac
- POSTGRES_USER=user
- POSTGRES_PASSWORD=1234
ports:
- 5432:5432
adminer:
image: adminer
restart: always
ports:
- 8080:8080
nginx:
build: ./nginx
image: nginx_db
container_name: nginx_db
restart: always
ports:
- "8004:8004"
- "8005:8005"
Dockerfile: (Dockerfile will later be used to copy ssl certs and keys)
FROM postgres:9.6
EXPOSE 5432
Wondering if there is something else I should be doing to enable this to work via some other utility?
Any help would be great.
Thanks in advance.
Update:
Tried accessing the database through the IP of the postgresql container 172.28.0.3 but the connection times out which suggests that PostgreSQL is correctly listening on 0.0.0.0:5432 and for some reason the user and password are not usable outside of Docker even from the host machine using localhost.
Check your pg_hba.conf file in the Postgres data folder.
The default configuration is that you can only login from localhost (which I assume Adminer is doing) but not from external IPs.
In order to allow access from all external addresses vi password authentication, add the following line to your pg_hba.conf:
host all all * md5
Then you can connect to your postgres DB running in the docker container from outside, given you expose the Port (5432)
Use the command docker container inspect ${container_number}, this will tell you which IPaddress:ports are exposed external to the container.
The command 'docker container ls' will help identify the 'container number'
After updating my default db_name, I also had to update the docker-compose myself by explicitly exposing the ports as the OP did
db:
image: postgres:13-alpine
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=1234
ports:
- 5432:5432
But the key here was restarting the server! DBeaver has connected to localhost:5432 :)