I've tried to add a postgis-vector-layer in qgis 2.14.0 by using authentication (authConfigId) and PyQGIS. When running the script I'm asked to login by username/password instead of using authConfigID. What is wrong with "uri.setConnection" ?

Example of the script:

uri.setConnection("host_name","5432","database_name", "4zoa91a" )  #"4zoa91a" = my_example_authConfigId
vLayer=QgsVectorLayer(uri.uri(),"layer_name", "postgres")
if vLayer.isValid() : print('layer is valid' )

In developer cookbook there is no hint to solve the problem. In the QGIS api-documentation I discovered:

void QgsDataSourceURI::setConnection    (   const QString &     aHost,
        const QString &     aPort,
        const QString &     aDatabase,
        const QString &     aUsername,
        const QString &     aPassword,
        SSLmode     sslmode = SSLprefer,
        const QString &     authConfigId = QString()

Here username/password are required, but as I said, I've tried to use the AuthentificationConfigID instead. (Is no problem to load this layer in qgis via interface.).

(Further it would be nice to get a hint/link how to "read"/"apply" the qgis-api-documentation/structure.)


1 Answer 1


As I noted in my comment, try:

uri.setConnection("host_name","5432","database_name", None, None, authConfigId = "4zoa91a")

This plugin has an example using this method:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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