Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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
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

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

Your Answer


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.