3

I am trying to develop a web app using spring boot and postgres. I used http://start.spring.io/ to create a starter spring project with Postgres SQL jdbc driver as a dependency. I have added the following in application.properties file

spring.datasource.url= jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password="mypassword@123"   
spring.jpa.hibernate.ddl-auto=validate

However, this is throwing the following error:

2017-07-30 19:09:15.168 ERROR 2959 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:443) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:217) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:215) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.Driver.makeConnection(Driver.java:404) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.Driver.connect(Driver.java:272) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.16.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.16.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735) [tomcat-jdbc-8.5.16.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667) [tomcat-jdbc-8.5.16.jar:na]

The password is not wrong. When I use psql to log in to the database using that password, it works.

The pg_hba.conf file is:

local   all             all                                     password
host    all             all             127.0.0.1/32            password
host    all             all             ::1/128                 password

I'm using java 1.8 with postgres SQL 9.6 on my macbook pro.

6
  • have you tried spring.datasource.password="mypassword" without @123 Commented Jul 30, 2017 at 13:56
  • 2
    Run this command ALTER USER "postgres" WITH PASSWORD 'mypassword@123'; and restart your application Commented Jul 30, 2017 at 14:04
  • Thank you for your responses. @user7294900 - Tried that just now. Unfortunately, it did not work. Commented Jul 30, 2017 at 14:29
  • @AjitSoman - This too did not work. :-( Commented Jul 30, 2017 at 14:29
  • 1
    Sorry. Meant without double quotes spring.datasource.password=mypassword Commented Jul 30, 2017 at 14:35

5 Answers 5

5

Your password should be a simple text (without adding characters as ")

 spring.datasource.password=mypassword
Sign up to request clarification or add additional context in comments.

3 Comments

Just adding a comment. In my case, my password contained "\", so I had to make it "\\" to escape the letter, so that my password works.
in my case, i had a space at the end and never realize until I saw lechat comment thanks dude
for me, it did work with password password. But it works fine after I changed from password to mypassword
0

I was getting this when running my application locally and did not manually create my db, user and password. After manually creating them and then rerunning the application worked.

Comments

0

I solved it, in my case, I needed to connect to PostgreSQL, and I had it running in Docker. The first step was to check if port 5432 was in use with the command: netstat -a which it was. The problem was that I also had PostgreSQL installed locally, and it had port 5432 open, so I stopped the process from Windows -> Services. That worked for me.

Comments

0

I had the same problem.

In my case I was attempting to connect to a Postgres docker container which I had mapped to port 5432 on my local machine. What I didn't realise was that I also had PostgreSQL Server installed locally on my machine, and running on that same port.

Go into the Services app if using Windows and check if PostgreSQL Server is running. If it is, stop the service and try run your application again.

1 Comment

Hi Jack! Your answer is almost the same as this: stackoverflow.com/a/77369743/1411160.
-1

in my case it was necessary to restart docker-compose one time after that the spring boot application connected to postgresql server without any issue.

docker-compose.yml:

volumes:
    pg-data:
    gradle-cache:

  services:
    db:
      image: postgres:14.8-alpine
      container_name: db
    volumes:
      - pg-data:/var/lib/postgresql/data
      - ./etc/postgres/initdb:/docker-entrypoint-initdb.d
    expose:
      - 5432
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=$SPRING_DATASOURCE_PASSWORD
      - PGDATA=/var/lib/postgresql/data/pg-data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: always

  builder:
    image: gradle:8.11.1-jdk17-jammy
    container_name: builder
    working_dir: /app
    volumes:
      - .:/app
      - gradle-cache:/root/.gradle
    command: gradle bootJar --stacktrace

  app:
    image: openjdk:17-jdk-slimthe app
    container_name: app
    working_dir: /app
    volumes:
      - .:/app
    expose:
      - 8080
    ports:
      - "8080:8080"
    command: java -jar build/libs/app.jar
    restart: on-failure
    depends_on:
      builder:
        condition: service_completed_successfully
      db:
        condition: service_healthy

init.sql:

CREATE TABLE IF NOT EXISTS person (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(128) NOT NULL,
    last_name VARCHAR(200) NOT NULL
);

.env:

DB_NAME=postgres
DB_HOST=db
SPRING_DATASOURCE_PASSWORD=<changeme>
SPRING_DATASOURCE_USERNAME=postgres
SPRING_DATASOURCE_URL=jdbc:postgresql://$DB_HOST:5432/$DB_NAME

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.