I wonder if it is possible to get the user and password for a PostGIS database if they are stored in the PostGis connection information.
I get the connection information at the moment from the active layer like this:
connInfo = QgsDataSourceURI(layer.dataProvider().dataSourceUri()).connectionInfo()
print connInfo
dbname='test_geodata_project' host=10.0.4.111 port=5432 sslmode=disable
userInfo = QgsCredentials.instance().get(connInfo, None, None)
print userInfo
(True, u'testuser', 'test')
But the user has to type in the user/password manually. Is there something like the connectionInfo()
but with user/password?
Best Answer
Yes, you can do that, but only if you chose to save user and password while establishing the PostGIS connection.
If you did so and your active layer is a PostGIS layer, you can access the connection information this way:
You'll get a key-value string like this:
From which you can obtain the password this way:
Include a similar
if
for the user into thefor
loop and you are done.