3

I have a plugin for QGIS that helps to populate databases. The user has the option to create a new database or to use an existing connection. If the user chooses to make a new database, I would also like to add this database as a new connection in the browser panel :

enter image description here

I can find lots of help on connecting to the database, which I have been able to do, but nothing for how to add a new connection. Is this possible?

3 Answers 3

4

I figured it out myself in the end. The database connection information is stored in PyQt4.QtCore.QSettings().

Using this page: http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html

I was able to add all of the required properties & credentials to the connection list.

2
  • is passing sensitive credentials such as password safe in this case ? Commented Dec 28, 2018 at 15:48
  • @AbhijitGujar the credentials are stored as plain text in the QGIS configuration file itself, which is not secure, but it requires that a user has already saved the connection and explicitly stated to do so. I think there is a clear warning popup in QGIS that advises against doing this for sensitive connections.
    – wfgeo
    Commented Jan 4, 2019 at 11:55
2

Since a few versions of QGIS (at least QGIS 3.16 for this API), there is a proper API for adding a database connection, without touching to the QgsSettings.

uri = QgsDataSourceUri()
uri.setConnection("HOST", "5432", "DATABASE", "USER", "PASSWORD")

config = {
  "saveUsername": True,
  "savePassword": True,
  "estimatedMetadata": True,
  "metadataInDatabase": True,
}

metadata = QgsProviderRegistry.instance().providerMetadata('postgres')
connection = metadata.createConnection(uri.uri(), config)
connection: QgsAbstractDatabaseProviderConnection
connection.store("new name")
# iface.browserModel().reload()

It's possible to use these intermediate variables :

For instance, it's possible to have the QgsDataSourceUri from a layer directly.

I'm not sure why some settings need to be a string or a proper boolean, the result is somehow different.

1

Faced the same case. If someone is looking for a complete solution here is a sample for adding a PostgreSQL:

settings = QSettings()

settings.setValue('PostgreSQL/connections/your_connection_name/host', 'host_name')
settings.setValue('PostgreSQL/connections/your_connection_name/database', 'database_name')
settings.setValue('PostgreSQL/connections/your_connection_name/username', 'user_name')
settings.setValue('PostgreSQL/connections/your_connection_name/password', 'password_name')
settings.setValue('PostgreSQL/connections/your_connection_name/port', port_number)
settings.setValue('PostgreSQL/connections/your_connection_name/allowGeometrylessTables', 'false') 
settings.setValue('PostgreSQL/connections/your_connection_name/authcfg', '')
settings.setValue('PostgreSQL/connections/your_connection_name/dontResolveType', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/estimatedMetadata', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/geometryColumnsOnly', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/projectsInDatabase', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/publicOnly', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/savePassword', 'true')
settings.setValue('PostgreSQL/connections/your_connection_name/saveUsername', 'true')
settings.setValue('PostgreSQL/connections/your_connection_name/service', '')
settings.setValue('PostgreSQL/connections/your_connection_name/sslmode', 'SslAllow')

iface.mainWindow().findChildren(QWidget, 'Browser')[0].refresh() # if connection is needed to be displayed immediately in browser

Params after port are from my connection setting and not set by default.

4
  • May is using QgsSettings() an option to be consistent with the QGIS documentation. Commented May 22, 2023 at 9:52
  • @Cartographer sorry, I can't understand what you mean Commented May 22, 2023 at 15:14
  • 1
    He is saying to use QgsSettings instead of QSettings : settings = QgsSettings()
    – etrimaille
    Commented Jun 13, 2023 at 9:50
  • 1
    For refreshing the browser in the graphical interface, your line can be replaced with iface.browserModel().reload()
    – etrimaille
    Commented Jun 13, 2023 at 9:52

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.