QGIS: Is it possible to edit the datasource of a PostGIS layer?
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
qgis.console.clearConsole()
layer = qgis.utils.iface.mapCanvas().currentLayer()
pro = layer.dataProvider()
print "The old providers datasource"
pro.dataSourceUri()
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"
pro.dataSourceUri()
print("Reloading data")
pro.reloadData()
layer.reload()
print "The layers datasource"
layer.publicSource()
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.
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.
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 ...