Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot conect to db through jdbc

Status
Not open for further replies.

x44446

Programmer
Mar 14, 2006
2
0
0
RU
I have postgresql 8.1 installed
db name is 'mydb'
user name is 'test'
password is 'test'

I can conect to this db by psql
$psql -U test mydb

But I cannon connect by jdbc
Code:
import java.io.*;
import java.sql.*;

public class Sample {
    Connection  conn;
    Statement   stmt;

    public Sample() throws ClassNotFoundException, FileNotFoundException, IOException, SQLException {

        Class.forName("org.postgresql.Driver");
        conn = DriverManager.getConnection("jdbc:postgresql:mydb", "test", "test");
        stmt = conn.createStatement();

        stmt.close();
        conn.close();
    }

    public static void main(String args[]) {

        try {
            Sample test = new Sample();
        } catch(Exception exc) {
            System.err.println("Exception caught.\n" + exc);
            exc.printStackTrace();
        }
    }
}


The error
Code:
$ java -cp /usr/local/share/java/classes/postgresql.jar:. Sample
Exception caught.
org.postgresql.util.PSQLException: FATAL: missing or erroneous pg_hba.conf file
org.postgresql.util.PSQLException: FATAL: missing or erroneous pg_hba.conf file
        at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:275)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:94)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
        at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:116)
        at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
        at org.postgresql.jdbc3g.Jdbc3gConnection.<init>(Jdbc3gConnection.java:24)
        at org.postgresql.Driver.makeConnection(Driver.java:369)
        at org.postgresql.Driver.connect(Driver.java:245)
        at java.sql.DriverManager.getConnection(DriverManager.java:525)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at Sample.<init>(Sample.java:11)
        at Sample.main(Sample.java:21)
 
additional info

postgresql.conf
Code:
listen_addresses = '*'

pg_hba.conf
Code:
local   all         all                               md5
host    all         all         127.0.0.1/32          md5
 
I guess that your problem is that pg_hba.conf file does not end with new line

open it and add after the second md5 a new line

the error should disappear
 
The entries you've shown in your pg_hba.conf should be tab- delimited, and no other whitespace chars, as shown below:

local<tab>all<tab>all<tab>md5
host<tab>all<tab>all<tab>127.0.0.1/32<tab>md5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top