I add a layer using an authentification config ID using PyQGIS:

uri = QgsDataSourceUri()
uri.setConnection(HOST, PORT, DB, None, None, QgsDataSourceUri.SslPrefer, pg_auth_id)
QgsVectorLayer(uri.uri(), "mylayer", "postgres")

In the project, the credentials are written in clear text in the project file:

<datasource>dbname='pub' host=geodb.rootso.org port=5432 user='xxxxxx' password='xxxxxx'

Is it possible to avoid this? How?

1 Answer 1


Solution is to call QgsDataSourceUri.uri with False:

QgsVectorLayer(uri.uri(False), "mylayer", "postgres")

see docs: https://qgis.org/pyqgis/master/core/QgsDataSourceUri.html#qgis.core.QgsDataSourceUri.uri

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.