Illegal UTF-8 Sequence Connecting to PostgreSQL Database

I have the following code to connect to a database

String host = "jdbc:postgresql://localhost:5432/name"; String username = "user"; String password = "pass"; Connection c = null; try { Class.forName("org.postgresql.Driver"); c = DriverManager.getConnection(host, username, password); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } System.out.println("Opened database successfully"); } 

and I get the following error:

 org.postgresql.util.PSQLException: El intento de conexión falló. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65) at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:149) at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:35) at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22) at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:47) at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:30) at org.postgresql.Driver.makeConnection(Driver.java:414) at org.postgresql.Driver.connect(Driver.java:282) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at database_console.DBConnect.main(DBConnect.java:22) Caused by: java.io.IOException: Illegal UTF-8 sequence: byte 2 of 4 byte sequence is not 10xxxxxx: 110 at org.postgresql.core.UTF8Encoding.checkByte(UTF8Encoding.java:28) at org.postgresql.core.UTF8Encoding.decode(UTF8Encoding.java:117) at org.postgresql.core.PGStream.ReceiveString(PGStream.java:327) at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:424) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203) ... 11 more org.postgresql.util.PSQLException: El intento de conexión falló. 

"el intento de conexión falló" means that the connection attempt failed.

Please help me with this, I do not know what to do.

EDIT: I also checked the encoding of the server and it says UTF8

+7
java sql postgresql utf-8
source share
5 answers

I think the problem may be that java strings are UTF-16 and Postgresql are UTF-8 parameters.

try this syntax:

 Properties props = new Properties(); props.setProperty("user","user"); props.setProperty("password","pass"); c = DriverManager.getConnection(host, props); 

hope this helps

+1
source share

I know this question is a bit outdated. But I had the same problem, and it turned out that this is a METHOD connection in the pg_hba.conf file.

I had a standard md5 configuration by default, but it looks like there is an error in some versions of the JDBC driver ( https://www.postgresql.org/message-id/3E43175C.5020209%40xythos.com )

So, I fixed it by simply changing md5 to trust in the pg_hba.conf file so that it accepts plaintext passwords.

host all all 127.0.0.1/32 trust

hope this helps someone

+1
source share

If you are on Windows, you need to use double slashes, for example:

  String host = "jdbc:postgresql:////localhost:5432//name"; 
0
source share

In my case, the problem was trivial, but the exception is unclear.

The only problem in the URL of my connection was DBNAME at the end of the connection. The name of the database simply did not exist, it was never created, and as the final result I received this feedback with an invalid character.

Make sure your connection settings are valid, double-check your database name in the URL. And before trying to programmatically connect to the database, use pgadmin to check for db.

0
source share

My solution to this problem: I am using the application.properties file to connect PostgreSQL , if the username is incorrect and there are two files in my project, then I have to change both.

0
source share

All Articles