You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository contains a [Juju Charm](https://charmhub.io/postgresql) for deploying [PostgreSQL](https://www.postgresql.org/about/) on virtual machines ([LXD](https://ubuntu.com/lxd)).
11
-
To deploy on Kubernetes, please use [Charmed PostgreSQL K8s Operator](https://charmhub.io/postgresql-k8s).
9
+
This repository contains a charmed operator for deploying [PostgreSQL](https://www.postgresql.org/about/) on virtual machines via the [Juju orchestration engine](https://juju.is/).
12
10
13
-
This operator provides a PostgreSQL database with replication enabled: one primary instance and one (or more) hot standby replicas. The Operator in this repository is a Python script which wraps PostgreSQL versions distributed by Ubuntu Jammy series and adding [Patroni](https://github.com/zalando/patroni) on top of it, providing lifecycle management and handling events (install, configure, integrate, remove, etc).
11
+
To learn more about how to deploy and operate Charmed PostgreSQL, see the [official documentation](https://canonical-charmed-postgresql.readthedocs-hosted.com/).
14
12
15
-
## README contents
13
+
## Overview
16
14
17
-
*[Basic usage](#basic-usage): Deploy and scale Charmerd PostgreSQL
18
-
*[Integrations](#integrations-relations): Supported interfaces for integrations
19
-
*[Contributing](#contributing)
20
-
*[Licensing and trademark](#licensing-and-trademark)
15
+
This operator provides a PostgreSQL database with replication enabled: one primary instance and one (or more) hot standby replicas. The Operator in this repository is a Python script which wraps PostgreSQL versions distributed by Ubuntu Jammy series and adding [Patroni](https://github.com/zalando/patroni) on top of it, providing lifecycle management and handling events (install, configure, integrate, remove, etc).
cursor.execute("SELECT usename FROM pg_catalog.pg_user;")
712
+
withself._connect_to_database(
713
+
database_host=host
714
+
) asconnection, connection.cursor() ascursor:
715
+
ifgroup:
716
+
query=SQL(
717
+
"SELECT usename FROM (SELECT UNNEST(grolist) AS user_id FROM pg_catalog.pg_group WHERE groname = {}) AS g JOIN pg_catalog.pg_user AS u ON g.user_id = u.usesysid;"
# Create database function and event trigger to identify users created by PgBouncer.
812
+
cursor.execute(
813
+
"SELECT TRUE FROM pg_event_trigger WHERE evtname = 'update_pg_hba_on_create_schema';"
730
814
)
815
+
ifcursor.fetchone() isNone:
816
+
cursor.execute("""
817
+
CREATE OR REPLACE FUNCTION update_pg_hba()
818
+
RETURNS event_trigger
819
+
LANGUAGE plpgsql
820
+
AS $$
821
+
DECLARE
822
+
hba_file TEXT;
823
+
copy_command TEXT;
824
+
connection_type TEXT;
825
+
rec record;
826
+
insert_value TEXT;
827
+
changes INTEGER = 0;
828
+
BEGIN
829
+
-- Don't execute on replicas.
830
+
IF NOT pg_is_in_recovery() THEN
831
+
-- Load the current authorisation rules.
832
+
DROP TABLE IF EXISTS pg_hba;
833
+
CREATE TEMPORARY TABLE pg_hba (lines TEXT);
834
+
SELECT setting INTO hba_file FROM pg_settings WHERE name = 'hba_file';
835
+
IF hba_file IS NOT NULL THEN
836
+
copy_command='COPY pg_hba FROM ''' || hba_file || '''' ;
837
+
EXECUTE copy_command;
838
+
-- Build a list of the relation users and the databases they can access.
839
+
DROP TABLE IF EXISTS relation_users;
840
+
CREATE TEMPORARY TABLE relation_users AS
841
+
SELECT t.user, STRING_AGG(DISTINCT t.database, ',') AS databases FROM( SELECT u.usename AS user, CASE WHEN u.usesuper THEN 'all' ELSE d.datname END AS database FROM ( SELECT usename, usesuper FROM pg_catalog.pg_user WHERE usename NOT IN ('backup', 'monitoring', 'operator', 'postgres', 'replication', 'rewind')) AS u JOIN ( SELECT datname FROM pg_catalog.pg_database WHERE NOT datistemplate ) AS d ON has_database_privilege(u.usename, d.datname, 'CONNECT') ) AS t GROUP BY 1;
842
+
IF (SELECT COUNT(lines) FROM pg_hba WHERE lines LIKE 'hostssl %') > 0 THEN
IF (SELECT COUNT(lines) FROM pg_hba WHERE lines = insert_value) = 0 THEN
852
+
INSERT INTO pg_hba (lines) VALUES (insert_value);
853
+
changes := changes + 1;
854
+
END IF;
855
+
END LOOP;
856
+
-- Remove users that don't exist anymore from the pg_hba file.
857
+
FOR rec IN SELECT h.lines FROM pg_hba AS h LEFT JOIN relation_users AS r ON SPLIT_PART(h.lines, ' ', 3) = r.user WHERE r.user IS NULL AND (SPLIT_PART(h.lines, ' ', 3) LIKE 'relation_id_%' OR SPLIT_PART(h.lines, ' ', 3) LIKE 'pgbouncer_auth_relation_%' OR SPLIT_PART(h.lines, ' ', 3) LIKE '%_user_%_%')
858
+
LOOP
859
+
DELETE FROM pg_hba WHERE lines = rec.lines;
860
+
changes := changes + 1;
861
+
END LOOP;
862
+
-- Apply the changes to the pg_hba file.
863
+
IF changes > 0 THEN
864
+
copy_command='COPY pg_hba TO ''' || hba_file || '''' ;
0 commit comments