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.

2 Answers 2


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.


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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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