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.
Related
My skill in Docker is little. I am using Docker desktop for Windows version 4.9.1 (81317) (latest version at this time). My web-app is multi-tenant system, need 3 databases. This is my docker-compose.yml (run 1 database ok)
version: '2.1'
services:
postgres:
image: postgres
ports:
- "5433:5432"
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: acc_spring
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d acc_spring"]
interval: 10s
timeout: 5s
retries: 3
Please guide me how to write/revise the above docker-composite.yml for running 3 databases at the same time:
tenant_master: port 5432, database name: tenant_master, username postgres, password postgres .
tenant_1: port 5432, database name: tenant_1, username postgres, password postgres .
tenant_2: port 5432, database name: tenant_2, username postgres, password postgres .
Notice: 3 databases in a database instance/server.
Please also explain for me what/how/ when to use restart: always, Do I have another option(s) for always?
Besides the port mapping (5433 seems wrong) you shouldn't need to change anything (well - probably the admin password shouldn't be 'postgres' in production).
You need to connect to this instance and create your databases (SQL or some app) and users. This is not related to Docker or docker compose. POSTGRES_DB is just the default database (not THE database).
However, if you want to do this as part of your container setup, you could do it like follows. Create a new project directory with these files:
File create-databases.sh
#!/bin/bash
set -eu
function create_database() {
local database=$1
echo " Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
create_database $POSTGRES_DB2
create_database $POSTGRES_DB3
File docker-compose.yml
services:
postgres:
image: postgres:latest
container_name: postgres
ports:
- "5433:5432"
restart: unless-stopped
volumes:
- ./create-databases.sh:/docker-entrypoint-initdb.d/create-databases.sh
- ./db_persist:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tenant_master
POSTGRES_DB2: tenant_1
POSTGRES_DB3: tenant_2
While this is not best practice (I'm not a Postgres user) it will get you started. By using volumes, an init script is put into a folder where it is picked up by container initialization and the database is persisted, just in case. I left out the health-check for better clarity. It can be re-added of course.
Having said this, there would be another solution: just copy the postgres service (postgres2, postgres3) to be used with different ports (5434, 5435). Although this would run 3 independent database instances (needs more ressources), it might have its advantages as well.
As regards the restart policy: instead of "always" you might consider "unless-stopped" (see https://docs.docker.com/engine/reference/run/#restart-policies---restart) like it is done in my suggestion above.
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.
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.
What I'm trying to do: connect to postgres db with a FastAPI app via common docker-compose file. It used to work until I changed postgres configuration from default.
I'm aware this is a common problem and I tried following every tip I found so far.
I did double check spelling of evn variables. I did remove volumes, images, networks and rebuild it from scratch multiple times by now.
Here are relevant parts of docker-compose
version: "3.4"
networks:
internal:
external: false
services:
db:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_USER=test_user
- POSTGRES_PASSWORD=test_pass
- POSTGRES_DB=test_db
networks:
- internal
my_app:
depends_on:
- db
networks:
- internal
Here's how I present db to app in code.
DATABASE_URL = "postgres://test_user:test_pass#db:5432/test_db"
I continue to get 'password authentication failed for user "test_user" / Role "test_user" does not exist. / Connection matched pg_hba.conf line 95: "host all all all md5"
What did I manage to miss?
For those who find it, the problem was actually in how I tried to provide env variables.
environment:
- POSTGRES_USER: test_user
- POSTGRES_PASSWORD: test_pass
- POSTGRES_DB: test_db
works
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.