[Qgis-user] Problem with auth config and connection to Oracle db
maryla GIS
maryla4gis at gmail.com
Mon Oct 20 00:23:40 PDT 2025
Hello everyone,
I am having trouble creating a new authentication configuration and
connecting to the Oracle database using the Python API. The new
authentication configuration and connection are created without errors, but
in the end, the connection does not use the encrypted configuration. When I
click on 'Edit connection,' the configuration is listed, but it is not
properly applied to the connection. As a result, the layers from the
database store the user's password in plain text. Additionally, the
connection schema is not properly saved despite using setSchema() method.
# https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/authentication.html#populate-authdb-with-a-new-authentication-configuration-entry
auth_mgr = QgsApplication.authManager()
auth_config = QgsAuthMethodConfig()
auth_config.setName('myconfig')
auth_config.setMethod('Basic')
auth_config.setConfigMap({
'username': login,
'password': password
})assert auth_config.isValid()
auth_mgr.storeAuthenticationConfig(auth_config)
auth_config_id = auth_config.id()assert auth_config_id
# https://gis.stackexchange.com/a/461589/173206
uri = QgsDataSourceUri()
uri.setConnection('myhost', '1521', 'mydb', None, None,
QgsDataSourceUri.SslPrefer, auth_config_id)
uri.setSchema('myschema')
conn_config = {
"saveUsername": True,
"savePassword": True,
"estimatedMetadata": True,
"metadataInDatabase": True,
"onlyExistingTypes": False,
}
metadata = QgsProviderRegistry.instance().providerMetadata('oracle')
connection = metadata.createConnection(uri.uri(False), conn_config) #
tried both True and False inside uri()
connection: QgsAbstractDatabaseProviderConnection
connection.store('My Connection')
iface.browserModel().reload()
[image: uncorrect_conn_settings.png]
[image: auth_config_list.png]
I don't have any issues creating the authentication configuration and
connecting to the PostgreSQL database this way – it uses the authentication
configuration correctly. The problem occurs when I try to connect to Oracle.
I've tested the code on Windows with QGIS versions 3.34, 3.40, and 3.44,
and also when creating a new user profile.
I've posted the question on gis.stackexchange.com
<https://gis.stackexchange.com/questions/495763/adding-new-authentication-configuration-and-connection-to-oracle-database-with-e>,
but I think this case might be a bit tricky.
I know that one possible solution is to set the connection parameters by
modifying QSettings/QgsSettings
<https://gis.stackexchange.com/a/451880/173206> - I've done it successfully
this way. However, I'm curious to know what's wrong with creating the
connection using the code above. Does anyone have any idea?
Best regards,
Maryla
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20251020/0ea2bc56/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: uncorrect_conn_settings.png
Type: image/png
Size: 33605 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20251020/0ea2bc56/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: auth_config_list.png
Type: image/png
Size: 8470 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20251020/0ea2bc56/attachment-0003.png>
More information about the QGIS-User
mailing list