I wrote the code below, but I need that the PostGIS connection to be verified, to be sure that the username and password are right. I tried this: How to access user credentials in database connection? , but it didn't worked.
I am using QGIS 3.8.3
uri = QgsDataSourceUri()
uri.setConnection('LPOSTDES-CL2', '5432', 'ugpi', None, None)
connInfo = uri.connectionInfo()
(success ,user, passwd) = QgsCredentials.instance().get(connInfo, None, None)
if success:
QgsCredentials.instance().put(connInfo, user, passwd)
uri.setConnection('LPOSTDES-CL2', '5432', 'ugpi', user, passwd)
(a, user, passwd)with(success, user, passwd), it works with me. – J. Monticolo Feb 12 '20 at 15:48print(uri.connectionInfo())– vitorcsm Feb 13 '20 at 11:01