1

I'm trying to test my Spring DAO level but cannot connect to my local database. Here is the Configuration.

Yes, I use port 54321 instead of standard port 5432:

@Bean
public DataSource dataSource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setPassword("password");
    dataSource.setUsername("postgres");
    dataSource.setUrl("jdbc:postgresql://localhost:53421/mydbtest");
    dataSource.setDriverClassName("org.postgresql.Driver");

    return dataSource;
}

@Bean 
public JdbcTemplate jdbcTemplate(){
    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource());

    return template;
}

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
{
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();    
    dataSourceInitializer.setDataSource(dataSource);        
    dataSourceInitializer.setEnabled(true);
     ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.addScript(new ClassPathResource("src/resource/sql/01_init_database.sql"));
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    return dataSourceInitializer;
}

I switched the firewall off, tried different versions of postgresql driver. But I stilll get this exception. And he also writes me to check my user-password-port data, but I'm sure they are correct:

Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)

What could be also wrong?

I use postgresql 9.2

UPDATE

I am on Windows if it matters

Here is the line for pg_hba.conf

host    all             all                 0.0.0.0/0           trust

(I also tried md5)

Here a Snippet form postgres.conf

listen_addresses = '*'      # what IP address(es) to listen on;
                # comma-separated list of addresses;
                # defaults to 'localhost'; use '*' for all
                # (change requires restart)
port = 54321                # (change requires restart)
max_connections = 100           # (change requires restart)
2
  • Is Postgres really running? Can you connect using psql or another SQL client? Does localhost actually resolve to an IP address? (does ping localhost work?) What is the value of listen_address in postgresql.conf Commented Feb 5, 2017 at 11:25
  • localhost is pinged. the value in postgresconf is listen_addresses = '*' Commented Feb 5, 2017 at 11:36

1 Answer 1

2

Try to connect to PostgreSQL server on localhost:

psql -h localhost -p 53421

Are you able to connect? If not, what is an error?


Edit your PostgreSQL configuration:

pg_hba.conf

host all all 0.0.0.0/0 md5

postgresql.conf

    listen_addresses='*'
port = 53421

and restart server.

(it's not connected to Spring, it's rather PostgreSQL configuration issue)

Sign up to request clarification or add additional context in comments.

7 Comments

you are right: I cannot connect to locanhost from postgres. but pgAdmin cannot connect
What's the error message? "psql: could not connect to server: Connection refused" ?
I added this line and as far as i understand a restarted the server (with a command pg_clt start -D data). But I receive a message from restarting the cannot fins a configuration file and from psql still connection refused
Switch -D means data directory, you have to place there a system path to PostgreSQL data directory (and probably "data" is not correct directory, is it?).
It was a right directora. It finds now a file, but still cannot connect. I added an edit to the main post to show my conf files
|

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.