diff --git a/src/relations/postgresql_provider.py b/src/relations/postgresql_provider.py index 2092613869..c0ff8bdcf9 100644 --- a/src/relations/postgresql_provider.py +++ b/src/relations/postgresql_provider.py @@ -109,6 +109,12 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None: f"{self.charm.primary_endpoint}:{DATABASE_PORT}", ) + # Set connection string URI. + self.database_provides.set_uris( + event.relation.id, + f"postgresql://{user}:{password}@{self.charm.primary_endpoint}:{DATABASE_PORT}/{database}", + ) + # Update the read-only endpoint. self.update_read_only_endpoint(event) diff --git a/tests/unit/test_postgresql_provider.py b/tests/unit/test_postgresql_provider.py index bb94b4bf60..fbca7fddfc 100644 --- a/tests/unit/test_postgresql_provider.py +++ b/tests/unit/test_postgresql_provider.py @@ -126,6 +126,7 @@ def test_on_database_requested(harness): "username": user, "password": "test-password", "read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432", + "uris": f"postgresql://{user}:test-password@postgresql-k8s-primary.None.svc.cluster.local:5432/{DATABASE}", "version": POSTGRESQL_VERSION, "database": f"{DATABASE}", } @@ -140,6 +141,7 @@ def test_on_database_requested(harness): assert harness.get_relation_data(rel_id, harness.charm.app.name) == { "data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}', "endpoints": "postgresql-k8s-primary.None.svc.cluster.local:5432", + "uris": f"postgresql://{user}:test-password@postgresql-k8s-primary.None.svc.cluster.local:5432/{DATABASE}", "read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432", } @@ -151,6 +153,7 @@ def test_on_database_requested(harness): "data": f'{{"database": "{DATABASE}", "extra-user-roles": "{EXTRA_USER_ROLES}"}}', "endpoints": "postgresql-k8s-primary.None.svc.cluster.local:5432", "read-only-endpoints": "postgresql-k8s-replicas.None.svc.cluster.local:5432", + "uris": f"postgresql://{user}:test-password@postgresql-k8s-primary.None.svc.cluster.local:5432/{DATABASE}", } # BlockedStatus due to a PostgreSQLGetPostgreSQLVersionError.