Skip to content

Commit f540cd1

Browse files
committed
processing: verify postgresql access and request credentials if necessary (fixes #12137)
1 parent e96b204 commit f540cd1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

python/plugins/processing/algs/gdal/OgrAlgorithm.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import re
2929
import os
30+
import psycopg2
3031

3132
from qgis.core import QgsDataSourceURI, QgsCredentials
3233

@@ -54,14 +55,28 @@ def ogrConnectionString(self, uri):
5455
# key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
5556
# table="t4" (geom) sql=
5657
dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
57-
connInfo = dsUri.connectionInfo()
58-
(success, user, passwd ) = QgsCredentials.instance().get(connInfo, None, None)
59-
if success:
60-
QgsCredentials.instance().put(connInfo, user, passwd)
61-
ogrstr = ("PG:dbname='%s' host='%s' port='%s' user='%s' password='%s'"
62-
% (dsUri.database(), dsUri.host(), dsUri.port(), user, passwd))
58+
conninfo = dsUri.connectionInfo()
59+
60+
conn = None
61+
while not conn:
62+
try:
63+
conn = psycopg2.connect(dsUri.connectionInfo())
64+
except psycopg2.OperationalError, e:
65+
(ok, user, passwd ) = QgsCredentials.instance().get(conninfo, dsUri.username(), dsUri.password())
66+
if not ok:
67+
break
68+
69+
dsUri.setUsername( user )
70+
dsUri.setPassword( passwd )
71+
72+
if not conn:
73+
raise RuntimeError('Could not connect to PostgreSQL database - check connection info')
74+
75+
QgsCredentials.instance().put(conninfo, user, passwd)
76+
ogrstr = "PG:%s" % dsUri.connectionInfo()
6377
else:
6478
ogrstr = unicode(layer.source()).split("|")[0]
79+
6580
return '"' + ogrstr + '"'
6681

6782
def ogrLayerName(self, uri):

0 commit comments

Comments
 (0)