Can't connect to my database from IntelliJ

Answered

I have a reactive postgres database defined as follows in my application.properties file:

spring.datasource.url=r2dbc:postgresql://psql-db:5432/db
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=*****
spring.datasource.password=*****

And here is my docker-compose.yml file:

version: "3.8"

services:
  psql-db:
    image: postgres
    container_name: psql-db
    restart: always
    environment:
      - POSTGRES_USER=*****
      - POSTGRES_PASSWORD=*****
      - POSTGRES_DB=db
    ports:
      - '5432:5432'

  spring-app:
    container_name: spring-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '8080:8080'
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://psql-db:5432/db
      - SPRING_DATASOURCE_USERNAME=*****
      - SPRING_DATASOURCE_PASSWORD=*****
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update
    depends_on:
      - psql-db

I'm trying to access it via the following properties:

But when I'm testing the connection, I get:
DBMS: PostgreSQL (no ver.)
Case sensitivity: plain=mixed, delimited=exact
The connection attempt failed.

0
33 comments
Avatar
Yaroslav Bedrov

Hello,

Does it work if you use “localhost” instead of “psql-db”?

0

Yes, “Test connection” will work, but I can't make valid queries to the db. It's like there is no schema defined. When I manage, somehow, to define a schema then the queries return empty values.

0
Avatar
Yaroslav Bedrov

Could you please attach screenshots with queries? Is it possible to open DB structure in “Databases” window? Is it possible to open table content by double click?

0

I'm switching to DataGrip, where the problem is the same when I use “localhost”. For the query not to produce an error, I had to link the “schema.sql” file that is in my IntelliJ project. In the db that is in my Docker container, I have for sure a song named ‘FOO’.

0
Avatar
Yaroslav Bedrov

Is needed table shown in “Database Explorer” window?  

0

The answer is yes, but it is only because I imported “schema.sql” via “SQL Scripts / Run SQL Script”. I don't think I would have to do that with the “real” DB, the one that is inside Docker.

0
Avatar
Yaroslav Bedrov

Is it possible to connect to DB using other client (pgadmin or dbeaver)? Will they show other results?

It looks like you have container with DB, but DB itself doesn't contain any data.

0

I have a.container with a DB and I surely have data in it. I added the data via a REST interface and checked that it's there.

0
Avatar
Yaroslav Bedrov

Is it shown properly if you use other DB clients?

0

With pgAdmin, I can't connect at all to the DB. It should not be so difficult to connect to a DB which is inside a Docker container.

0
Avatar
Yaroslav Bedrov

Please try to run “Select * from song”. What results will be shown? What is shown if you try to get table content via REST?

0

“SELECT * FROM song” returns empty results. Important: When I configure the DB with “localhost” in DataGrip, I have to load the “schema.sql” file, otherwise the “song” table is not recognized. But regarding the “real” DB table that is in Docker: it already have a schema defined when the Spring app starts in the container.

0
Avatar
Yaroslav Bedrov

Could you please try to connect to DB from the container using command line: https://medium.com/@bennokohrs/connect-to-postgresql-database-inside-docker-container-7dab32435b49

Will it work and show table content? 

0

I have followed the instructions and “SELECT * FROM song” returns nothing on the docker postgress image.

Here is the log related to the db when “docker compose up” is starting:
psql-db     | The files belonging to this database system will be owned by user "postgres".
psql-db     | This user must also own the server process.
psql-db     | 
psql-db     | The database cluster will be initialized with locale "en_US.utf8".
psql-db     | The default database encoding has accordingly been set to "UTF8".
psql-db     | The default text search configuration will be set to "english".
psql-db     | 
psql-db     | Data page checksums are disabled.
psql-db     | 
psql-db     | fixing permissions on existing directory /var/lib/postgresql/data ... ok
psql-db     | creating subdirectories ... ok
psql-db     | selecting dynamic shared memory implementation ... posix
psql-db     | selecting default max_connections ... 100
psql-db     | selecting default shared_buffers ... 128MB
psql-db     | selecting default time zone ... Etc/UTC
psql-db     | creating configuration files ... ok
psql-db     | running bootstrap script ... ok
psql-db     | performing post-bootstrap initialization ... ok
psql-db     | initdb: warning: enabling "trust" authentication for local connections
psql-db     | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
psql-db     | syncing data to disk ... ok
psql-db     | 
psql-db     | 
psql-db     | Success. You can now start the database server using:
psql-db     | 
psql-db     |     pg_ctl -D /var/lib/postgresql/data -l logfile start
psql-db     | 
psql-db     | waiting for server to start....2024-06-23 16:53:52.716 UTC [48] LOG:  starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
psql-db     | 2024-06-23 16:53:52.719 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
psql-db     | 2024-06-23 16:53:52.731 UTC [51] LOG:  database system was shut down at 2024-06-23 16:53:52 UTC
psql-db     | 2024-06-23 16:53:52.737 UTC [48] LOG:  database system is ready to accept connections
psql-db     |  done
psql-db     | server started
psql-db     | CREATE DATABASE
psql-db     | 
psql-db     | 
psql-db     | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
psql-db     | 
psql-db     | 2024-06-23 16:53:52.952 UTC [48] LOG:  received fast shutdown request
psql-db     | waiting for server to shut down....2024-06-23 16:53:52.955 UTC [48] LOG:  aborting any active transactions
psql-db     | 2024-06-23 16:53:52.958 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
psql-db     | 2024-06-23 16:53:52.958 UTC [49] LOG:  shutting down
psql-db     | 2024-06-23 16:53:52.961 UTC [49] LOG:  checkpoint starting: shutdown immediate
psql-db     | 2024-06-23 16:53:53.054 UTC [49] LOG:  checkpoint complete: wrote 922 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.026 s, sync=0.058 s, total=0.096 s; sync files=301, longest=0.010 s, average=0.001 s; distance=4255 kB, estimate=4255 kB; lsn=0/1912038, redo lsn=0/1912038
psql-db     | 2024-06-23 16:53:53.060 UTC [48] LOG:  database system is shut down
psql-db     |  done
psql-db     | server stopped
psql-db     | 
psql-db     | PostgreSQL init process complete; ready for start up.
psql-db     | 
psql-db     | 2024-06-23 16:53:53.215 UTC [1] LOG:  starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
psql-db     | 2024-06-23 16:53:53.217 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
psql-db     | 2024-06-23 16:53:53.218 UTC [1] LOG:  listening on IPv6 address "::", port 5432
psql-db     | 2024-06-23 16:53:53.222 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
psql-db     | 2024-06-23 16:53:53.233 UTC [64] LOG:  database system was shut down at 2024-06-23 16:53:53 UTC
psql-db     | 2024-06-23 16:53:53.242 UTC [1] LOG:  database system is ready to accept connections

psql-db     | 2024-06-23 16:58:53.276 UTC [62] LOG:  checkpoint starting: time
psql-db     | 2024-06-23 16:58:57.538 UTC [62] LOG:  checkpoint complete: wrote 45 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.246 s, sync=0.008 s, total=4.263 s; sync files=12, longest=0.004 s, average=0.001 s; distance=260 kB, estimate=260 kB; lsn=0/1953458, redo lsn=0/1953420
psql-db     | 2024-06-23 17:03:52.271 UTC [103] FATAL:  role "postgres" does not exist

Can you spot something abnormal?

One thing is sure: my REST interface works and can write into “some” database.

0
Avatar
Yaroslav Bedrov

Could you please check list of available tables using DB client from the container?

According to provided data,  DB is created, but no tables or data is added. Does your Spring app shows table content just after starting container? Isn't there any data generated in application initialisation code? 

0

In the container, “SHOW TABLES” returns nothing.

- I'm not showing any table just after starting the container|.
- No data is generating during application initialization.

0
Avatar
Yaroslav Bedrov

It looks strange that there is no data shown in DB client, but Application could get it.

0

I posted something but i somehow disappeared. Trying again…

In another Spring project, everything is working as intended with my DB and Datagrip. The repository looks like this:
public interface SongRepository extends JpaRepository<Song, Long>
{
    Optional<Song> findByName(String name);
    Optional<Song> findByComposer(String composer);
}

In my other project which is not working, the repository looks like this:
public interface SongRepository extends ReactiveCrudRepository<Song, Long> {
    @Query("SELECT * FROM song WHERE name = :name")
    Flux<Song> findByName(String name);

    @Query("SELECT * FROM song WHERE composer = :composer")
    Flux<Song> findByComposer(String composer);
}

It looks like IntelliJ and Datagrip have a problem handling reactive databases?

0
Avatar
Yaroslav Bedrov

Do you have the same DB settings in application which is shown fine in DataGrip?

0

The docker-compose.xml is exactly the same (see in on of my earlier posts), but the application properties files are different:

spring.datasource.url=jdbc:postgresql://psql-db:5432/db
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=*****
spring.datasource.password=*****

spring.datasource.url=r2dbc:postgresql://psql-db:5432/db
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=*****
spring.datasource.password=*****

Even though I'm not sure these files are taken in count at all.

0

Hi Ariel. Does it make a difference if you use jdbc instead of r2dbc in the spring.datasource.url property for the reactive project?

0

Hi Arina, it doesn't make any difference.
I had doubts before, but now I'm sure: the application property file is not taken in count, at least the “spring.datasource” values.

0
I see, thank you. 
In that case, could you please upload application logs to https://uploads.jetbrains.com and provide the id of the upload here? I'll check the exceptions and concurrent events.
Help | Show Log in Finder -> idea.log file
0

Not sure I understand what you're asking. The idea.log file is huge and contains non-relevant information in my opinion.

So I have uploaded the log produced by “docker compose up” where you can see what's going on with the “psql_db” and “spring_app” images.

Upload id: 2024_07_09_5FLU1jUfpcYS5B1Rpv7phK (file: log.txt)

0
Please provide a minimal sample project so I can reproduce the issue without including your sensitive code. Thank you! Could you upload it to https://uploads.jetbrains.com (https://uploads.jetbrains.com/) and provide the upload ID here?
0

I uploaded a minimal project: Upload id: 2024_07_18_k1VSAeUCx3ChZ96dBrHJqB (file: JAVA.zip)

0

Hi, please try setting the host value to 0.0.0.0 in the IDEA Database toolwindow. Then, the database should be able to connect to it.

The reason is that if you check the log in the docker db image, the listen address is 0.0.0.0:

2024-07-23 22:38:47 2024-07-23 14:38:47.731 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-07-23 22:38:47 2024-07-23 14:38:47.731 UTC [1] LOG:  listening on IPv6 address "::", port 5432

0

I followed your instructions and the “Test Connection” buttons shows “Succeeded”. But when I try to access the content of the DB, I get an error, as if the schema is not known:

0

The table must be created before it can be selected. Please execute SQL in the file src/main/resources/schema.sql first:

CREATE TABLE IF NOT EXISTS song (id SERIAL PRIMARY KEY, name VARCHAR(255), composer VARCHAR(255));

You can open the file and execute it directly in IDEA.

0

I don't understand why I need to create a table in Datagrip. The table is already created by my Spring application, and I'm able to add a new song to the DB and check the content of the table via Postman.

0

Please sign in to leave a comment.