After creating a layer with RT_SQL_Layer plugin, I want to change some details in the query. Is this possible, or do I need to create a new layer every time I want to change something in the query?

More specifically; I want to change the "Source for This Layer" as shown in example bellow:

enter image description here

  • This plugin is now deprecated. See plugins.qgis.org/plugins/rt_sql_layer. Now after connecting to the PostGIS database and selecting the table to add into the project, press 'Set Filter'. A new window opens up where you can select what subset of the data you wish to bring into the project. This has to be done each time if you want the PostGIS layer to show different data Mar 17, 2017 at 6:49

4 Answers 4


EDIT: This is not ideal but it should work if you don't need to do it in the UI. You can open and edit the qgs project files as the connection strings and sql string is stored in there. When using postgres search for <datasource> in the qgs project file. As the file is xml you can just edit the string and reload the project file.

via Python:

You can kind of get half way there using the python APIs although you need to set the layers providers datasource and also the datasource on the layer itself, which you can't do......yet.

from PyQt4.QtCore import QString
layer = qgis.utils.iface.mapCanvas().currentLayer()
pro = layer.dataProvider()
print "The old providers datasource"
print "Set the providers datasource"
pro.setDataSourceUri(QString(u'dbname=\'postgis\' host=localhost port=5432 user=\'postgres\' key=\'property_no\' table="(SELECT \\"t_0\\".\\"property_no\\", \\"t_0\\".\\"SP_GEOMETRY\\" FROM \\"public\\".\\"Property\\" AS \\"t_0\\"\n)" (SP_GEOMETRY) sql='))
print "Print out the new datasource"

print("Reloading data")

print "The layers datasource"
print "God dam! It's still the old one"

It seems from the code there are two places where there is a datasource string; in the provider and in the layer. The layers datasource is not sync to the providers datasource, so you can update the provider but the layer still knows nothing.

I'll have a chat to the devs to see if there is a reason why we couldn't sync the two together.

  • Thank you, at this point is maybe an workaround (with some restrictions) would be to copy the original layer datasource string, modify it, delete the old layer and create a new one...
    – Pablo
    Mar 14, 2011 at 14:19
  • So it looks like the postgres data provider (or any of them for that matter) doesn't implement the reloadData method from its base object, which is why the URI never makes it's way to the layer above.
    – Nathan W
    May 10, 2011 at 1:21

You cannot change the query used in RT SQL Layer plugin. But you can try to only use a general query in RT and refine it in Layer Properties - General - Query builder. The part in Query builder can be easily adjusted while working with the same layer.

  • Thats one solution. But for the given example, where the purpose of the query is to create a 3 line label the problem persists. Do you know if it can be changed with python?
    – Pablo
    Feb 15, 2011 at 17:31

The quickest way is to open your QGIS project file (.qgs) in your favorite text editor (e.g., Notepad.exe, vim, whatever), and do a text search-replace for the connection string fragment.

For example, in Notepad.exe, go to Edit > Replace ... Text replace


The "Change DataSource Plugin" is for you :


Right click on layer tree to change single layer datasource of click on icon to change datasources globally

The plugin allows to change the datasources of the loaded layers. The operation can be performed right clicking on legend items for single layer or globally on a summary table by clicking on toolbar plugin icon. The plugin takes control of bad layers handling allowing to specify valid datasources working on project

Works with a right click on the layer :

change data source

You will have this type of text box :

PostGis datasource change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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