homelab/roles/postgresql/files/create-user.sql

11 lines
384 B
MySQL
Raw Normal View History

2024-09-29 18:04:34 +05:00
CREATE OR REPLACE FUNCTION create_user_if_not_exists(username TEXT, passwd TEXT)
RETURNS VOID AS \$\$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = username) THEN
EXECUTE format('CREATE ROLE %I LOGIN PASSWORD %L', username, passwd);
ELSE
EXECUTE format('ALTER ROLE %I WITH PASSWORD %L', username, passwd);
2024-09-29 18:04:34 +05:00
END IF;
END;
\$\$ LANGUAGE plpgsql;