Files
teleport/lib/srv/db/postgres/sql/remove-permissions.sql
T
Krzysztof Skrzętnicki d6915c8dea Implement granular db permissions for Postgres (RFD 151) (#37808)
* Implement granular db permissions for Postgres (RFD 151)

* docs, whitespace

* Make sure query is closed, even in case of errors along the way

* fix imports
2024-03-04 19:28:22 +00:00

18 lines
762 B
PL/PgSQL

CREATE OR REPLACE PROCEDURE pg_temp.teleport_remove_permissions(username VARCHAR)
LANGUAGE plpgsql
AS $$
DECLARE
row_data RECORD;
BEGIN
-- Check active connections.
IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username AND datname = current_database()) THEN
RAISE NOTICE 'User has active connections to current database';
ELSE
-- Loop through table permissions
FOR row_data IN (SELECT DISTINCT table_schema, table_name FROM information_schema.table_privileges WHERE grantee = username)
LOOP
EXECUTE 'REVOKE ALL PRIVILEGES ON ' || quote_ident(row_data.table_schema) || '.' || quote_ident(row_data.table_name) || ' FROM ' || quote_ident(username);
END LOOP;
END IF;
END;
$$;