Skip to content

Instantly share code, notes, and snippets.

@t-book
Last active August 26, 2024 11:23
Show Gist options
  • Save t-book/2e4cf7d85a2499528f9bf7e128d9b7c1 to your computer and use it in GitHub Desktop.
Save t-book/2e4cf7d85a2499528f9bf7e128d9b7c1 to your computer and use it in GitHub Desktop.
from qgis.core import QgsProject, QgsDataSourceUri
project = QgsProject.instance()
old_host = "10.130.0.3"
new_host = "development.com"
for layer_id, layer in project.mapLayers().items():
if layer.providerType() == 'postgres':
uri = QgsDataSourceUri(layer.source())
if uri.host() == old_host:
current_uri = uri.uri()
new_uri = current_uri.replace(f"host={old_host}", f"host={new_host}")
layer.setDataSource(new_uri, layer.name(), 'postgres')
print(f"Updated layer: {layer.name()}")
project.write()
print("All layers have been updated and the project has been saved.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment