Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm writing python plugin and trying to find the right way to add PostGIS layer on the map without storing user password. After adding layer on the map, I strip out the password from uri like this:

vlayer.dataProvider().setDataSourceUri(uri.removePassword(uri.uri()))

However, when user saves the project, full connection string stays including password string.

I know that QGIS can popup user name/password dialog for me if I don't put these data on uri, but then I need to get somehow entered data, since I need to know user name and password anyway.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

When a project is saved, QGIS uses the source connection information stored in a protected attribute (mDataSource) that is set when the layer is loaded. The value of mDataSource is not affected by setting the data source URI as you are doing in your code.

At present, there is no method that allows you to change the "internal" mDataSource, thus the only way to not store the password is to configure it that way in your PostgreSQL connection.

share|improve this answer
add comment

It would be better if you ask the user password. You can assign a variable and get the value of the variable from user with a dialog. refer to the link please How to connect a PyQGIS plugin with Postgres?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.