1

for my problem I just found this Question [Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections but it didn't help me very much. I tried everything they suggested.

I use

  • a VM with Ubuntu 12.04 LTS Server
  • PostgreSQL 9.3
  • Client with Win7
  • eclipse Keppler

I also used mySQL for testing purpose and everything works fine (so connection to VM etc is possible)

My LOG:

Jul 07, 2015 12:49:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Jul 07, 2015 12:49:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: de/motza/init/hibernatePSQL.cfg.xml
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: de/motza/init/hibernatePSQL.cfg.xml
Jul 07, 2015 12:49:35 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://10.158.251.251:5432/history_log]
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=postgres, password=****}
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

My Error:

Initial SessionFactory creation failed.
org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at de.motza.init.HibernateUtil.getSessionFactoryPSQL(HibernateUtil.java:35)
    at de.motza.test.History_Log.testPSQL(History_Log.java:124)
    at de.motza.test.History_Log.main(History_Log.java:99)
Caused by: org.postgresql.util.PSQLException: Connection to 10.158.251.251:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:239)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:127)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:41)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:414)
    at org.postgresql.Driver.connect(Driver.java:282)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    ... 15 more
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)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121)
    ... 24 more

my hibernate config

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://10.158.251.251:5432/history_log</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!--  create -> Drop the existing tables and create new one -->
        <!--  create-drop -> Drop the existing tables and create new one, when sessionFactory closes, the table will be dropped -->
        <!--  update -> updates table values, but saves data (not in production) -->
        <!--  validate -> validate existing table and not update or change database, throws exception -->
        <!--  for production leave this method out -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Mention here all the model classes along with their package name -->
        <mapping class="de.motza.test.LogEntry"/>
        <mapping class="de.motza.test.Parameter"/>


    </session-factory>
</hibernate-configuration>

My Java code creating the session factory:

if (sessionFactoryPSQL == null || sessionFactoryPSQL.isClosed()) {
            Configuration conf = new Configuration().configure("de/motza/init/hibernatePSQL.cfg.xml");
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            try {
                sessionFactoryPSQL = conf.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                System.err.println("Initial SessionFactory creation failed.");
                e.printStackTrace();
                System.exit(0);
            }
        }
        return sessionFactoryPSQL;

I also used sudo netstat -taupen | grep postgres

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      105        13970       3013/postgres
tcp6       0      0 ::1:5432                :::*                    LISTEN      105        13969       3013/postgres
udp6       0      0 ::1:37902               ::1:37902               ESTABLISHED 105        13981       3013/postgres

my pg_hba.conf looks like this:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

I also tried a different port but it is still the same Error.

It is part of my Bachelor Thesis and I do not know what to do anymore... Hope someone can help me

Community
  • 1
  • 1
Ruth
  • 856
  • 2
  • 13
  • 26

3 Answers3

3

Postgres is listening on 127.0.0.1:5432, but you're trying to connect to 10.158.251.251:5432. Even if those IP addresses resolved to the same machine, they won't be equal in the eyes of the postmaster (the first is a local connection, the second isn't).

Add the other IP to your pg_hba.conf as well.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • @Kaymaman I thought the line `host all all 0.0.0.0/0 md5` would do the job? In postgresql.conf I set the listen_address to '*' – Ruth Jul 08 '15 at 10:35
  • Well I'm assuming that your computer is in the 10.* etc. subnet, so it has a network address starting with 10 (hopefully the one you're trying to connect to). You need that in pg_hba.conf. You can test with `psql` trying to connect to your server. If `psql` can connect to it, then your Java program can too. – Kayaman Jul 08 '15 at 11:05
  • @Kaymaman Ok i tried with this line : `host all all 10.158.0.0/16 md5` but it still dowsn't work. The computer I work with has the IP 10.158.222.222 and the server has 10.158.251.251, ssh connection works just fine – Ruth Jul 08 '15 at 11:12
  • Ssh has nothing to do with anything here. Are you sure about the netmask? – Kayaman Jul 08 '15 at 11:22
  • And a firewall isn't blocking connections to port 5432? – Kayaman Jul 08 '15 at 12:29
1

in addition to @Kaymaman answer in linux environment selinux might be enable preventing web application to connect to the database. You can double check it by temporary disabling selinux using the command setenforce 0

Paizo
  • 3,986
  • 30
  • 45
0

haha I found the problem, how embarrassing, the line "listen_addresses" was commented out :)

Ruth
  • 856
  • 2
  • 13
  • 26