I'm trying to use the database, but I can't connect to it, I haven't found any solutions on the Internet, so please tell me what the problem is.
docker-compose.yml
services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: "monitorService"
POSTGRES_USER: "username"
POSTGRES_PASSWORD: "12345"
ports:
- "5432:5432"
volumes:
- ./src/main/resources/volume:/var/lib/postgresql/data
MigrationUtil
package org.monitoringservice.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class MigrationUtil {
public static void migrateDB(){
Properties properties = PropertiesUtil.getApplicationProperties();
try (Connection connection = DriverManager.getConnection(
properties.getProperty("url"),
properties.getProperty("username"),
properties.getProperty("password")))
{
System.out.println(connection.getSchema());
}catch (SQLException e){
System.out.println(e.getMessage());
}
}
}
PropertiesUtil
package org.monitoringservice.util;
import java.io.IOException;
import java.util.Properties;
public class PropertiesUtil {
public static Properties getApplicationProperties(){
Properties p = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
p.load(classLoader.getResourceAsStream("application.properties"));
}catch (IOException e){
System.out.println(e.getMessage());
}
return p;
}
}
application.properties
url=jdbc:postgresql://localhost:5432/monitorService
username=username
password=12345
changeLogFile=liquibase/changelog.xml
defaultSchemaName=monitoring
I tried to recreate the database, change the port. I tried to switch the database encoding, but without success.
The problem was two things.
First, if you had PostgreSQL installed, then stop its service, in my case, it was called
postgresql-x64-16 - PostgreSQL Server 16
After that, the errors began to be displayed correctly, and not in strange characters, as in the picture in the question. And also logs began to arrive in the docker container.
Secondly, I used docker-compose.yml and indicated where my volumes are located:
./src/main/resources/volume:/var/lib/postgresql/data
And in the logs of the database container I had:
PostgreSQL Database directory appears to contain a database; Skipping initialization
If you have the same log, then you need to delete volumes. I just deleted the contents of the volume folder in my project and then restarted docker.compose. You can also delete volume via commands.