Skip to content

Commit

Permalink
[PostgreSQL] Pass client_encoding='UTF-8' in connection string (fixes #…
Browse files Browse the repository at this point in the history
…41132)

to avoid issues with non-ASCII passwords, and local encoding different from
database encoding.

(cherry picked from commit d63717b)
  • Loading branch information
rouault authored and nyalldawson committed Feb 19, 2021
1 parent 2bf2246 commit e5a4e04
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -238,7 +238,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
QgsDataSourceUri uri( conninfo );
QString expandedConnectionInfo = uri.connectionInfo( true );

auto addDefaultTimeout = []( QString & connectString )
auto addDefaultTimeoutAndClientEncoding = []( QString & connectString )
{
if ( !connectString.contains( QStringLiteral( "connect_timeout=" ) ) )
{
Expand All @@ -247,10 +247,12 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
int timeout = settings.value( QStringLiteral( "PostgreSQL/default_timeout" ), PG_DEFAULT_TIMEOUT, QgsSettings::Providers ).toInt();
connectString += QStringLiteral( " connect_timeout=%1" ).arg( timeout );
}

connectString += QStringLiteral( " client_encoding='UTF-8'" );
};
addDefaultTimeout( expandedConnectionInfo );
addDefaultTimeoutAndClientEncoding( expandedConnectionInfo );

mConn = PQconnectdb( expandedConnectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
mConn = PQconnectdb( expandedConnectionInfo.toUtf8() );

// remove temporary cert/key/CA if they exist
QgsDataSourceUri expandedUri( expandedConnectionInfo );
Expand Down Expand Up @@ -314,8 +316,8 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s

QgsDebugMsgLevel( "Connecting to " + uri.connectionInfo( false ), 2 );
QString connectString = uri.connectionInfo();
addDefaultTimeout( connectString );
mConn = PQconnectdb( connectString.toLocal8Bit() );
addDefaultTimeoutAndClientEncoding( connectString );
mConn = PQconnectdb( connectString.toUtf8() );
}

if ( PQstatus() == CONNECTION_OK )
Expand All @@ -333,22 +335,6 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
return;
}

//set client encoding to Unicode because QString uses UTF-8 anyway
QgsDebugMsgLevel( QStringLiteral( "setting client encoding to UNICODE" ), 2 );
int errcode = PQsetClientEncoding( mConn, QStringLiteral( "UNICODE" ).toLocal8Bit() );
if ( errcode == 0 )
{
QgsDebugMsgLevel( QStringLiteral( "encoding successfully set" ), 2 );
}
else if ( errcode == -1 )
{
QgsMessageLog::logMessage( tr( "error in setting encoding" ), tr( "PostGIS" ) );
}
else
{
QgsMessageLog::logMessage( tr( "undefined return value from encoding setting" ), tr( "PostGIS" ) );
}

QgsDebugMsgLevel( QStringLiteral( "Connection to the database was successful" ), 2 );

deduceEndian();
Expand Down

0 comments on commit e5a4e04

Please sign in to comment.