PGJDBC: could not receive data from client:

5.2k Views Asked by At

Dear StackOverFlowers,

I was trying event-driven LISTENER/NOTIFY on Postgres 9.6 (Windows 10).

I followed PGJDBC example given by Steve Taylor at https://www.openmakesoftware.com/postgresql-listen-notify-events-example/.

I started by downloading pgjdbc-ng-0.7-complete.jar and have put that in my CLASSPATH replacing standard JDBC driver.

When I am trying to connect to Postgres database using pgjdbc driver, I am getting an error:

connection received: host=127.0.0.1 port=50325

connection authorized: user=postgres database=scott

could not receive data from client: An existing connection was forcibly closed by the remote host.

Here are my system variables:

DBHost: localhost

DBName: scott

DBPort: 5432

DBUserName: postgres

DBPassword: postgres

I am not getting past the first hurdle, rest looks like Mount Everest. Please help me. Should you be needing the code, I am following Steve's code ditto.

Further to Joseph Larson's answer, the database is always running. I have connected to Postgres database from PGADMIN and Java successfully. I think issue is with the connect string. From Java when I am using standard JDBC which is provided by Postgres I am using URL like jdbc:postgresql://localhost:5432/dbname but PGJDBC suggests a different connect string like JDBC:PGSQL://localhost:5432/dbname. I tried to connect with that string (forcibly), it did not work. There is no method in PGJDBC PGDataSource for providing URL directly. I had to go through:

dataSource.setHost(DBHost);

dataSource.setPort(5432);

dataSource.setDatabase(DBName);

dataSource.setUser(DBUserName);

dataSource.setPassword(DBPassword);

And what URL it is sending to Database I am not able to figure out. Please suggest me a connect string and this problem is solved.

thanks

Thanks very much for asking me to post error messages:

Exception in thread "main" java.lang.NullPointerException                                                
        at com.impossibl.postgres.system.BasicContext.loadLocale(BasicContext.java:294)                  
        at com.impossibl.postgres.system.BasicContext.init(BasicContext.java:273)                        
        at com.impossibl.postgres.jdbc.PGConnectionImpl.init(PGConnectionImpl.java:251)                  
        at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:182)          
        at com.impossibl.postgres.jdbc.AbstractDataSource.createConnection(AbstractDataSource.java:723)  
        at com.impossibl.postgres.jdbc.PGDataSource.getConnection(PGDataSource.java:66)                  
        at com.impossibl.postgres.jdbc.PGDataSource.getConnection(PGDataSource.java:58)                  
        at PGListenNotify.<init>(PGListenNotify.java:26)                                                 
        at PGListenNotify.main(PGListenNotify.java:37)          

Here is source code:

import java.sql.SQLException;
import java.sql.Statement;

import com.impossibl.postgres.api.jdbc.PGConnection;
import com.impossibl.postgres.jdbc.PGDataSource;

public class PGListenNotify
{
PGConnection connection;

public PGListenNotify()
{
String DBHost = System.getenv("DBHost");
String DBName = System.getenv("DBName");
String DBUserName = System.getenv("DBUserName");
String DBPassword = System.getenv("DBPassword");
try
{
PGDataSource dataSource = new PGDataSource();
dataSource.setHost(DBHost);
dataSource.setPort(5432);
dataSource.setDatabase(DBName);
dataSource.setUser(DBUserName);
dataSource.setPassword(DBPassword);

connection = (PGConnection) dataSource.getConnection();
Statement statement = connection.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
PGListenNotify ln = new PGListenNotify();
}
}                             
2

There are 2 best solutions below

0
On BEST ANSWER

This looks like the Windows locale bug in pgjbdc-ng. It has been addressed, try the latest version 0.8.1.

The latest releases have detailed documentation related to asynchronous notifications here.

If it still fails to execute on your Windows system, please create an issue here.

0
On

Did you actually start a database server? I didn't know PostgreSQL server could run on Windows, but I've never tried.

I would simplify your problem a little. I know nothing about psql on Windows, but on Mac, I would start the server and then use the psql command (it's part of PostgreSQL) to ensure the server was up and running.

If you're to connecting, then the problems can be:

-There is no server at all -The server isn't running on the port you're attempting -The server isn't listening for connections on host 127.0.0.1 but could be listening on the actual IP address of your machine -I'm not sure about that particular error, but username, password, or database may not exist.

I'd use psql to figure out which of those possible reasons is the real problem. That isolates out your program as being part of the problem, and it becomes entirely one of managing your database server.