Last active
August 26, 2024 11:23
-
-
Save t-book/2e4cf7d85a2499528f9bf7e128d9b7c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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