I have a use case where I want to create a function which i can use on every database available on my PostgreSQL instance so I can give execute grant to a non-super user.
Task of this function would be to get the user password hash from pg_shadow.
I will add this user to Pg bouncer user list to access user match user hashes and give them access to the PostgreSQL instance.
I don't have much experience working with databases. So it would be great help
I would also appreciate an alternative approach
Function I want to use in every database
CREATE OR REPLACE FUNCTION user_search(uname TEXT) RETURNS TABLE (usename name, passwd text) as
$$
WITH myuser as (SELECT $1 as fullusername) SELECT fullusername as usename, passwd FROM pg_catalog.pg_shadow INNER JOIN myuser ON usename = substring(fullusername from '[^@]*')
$$
LANGUAGE sql SECURITY DEFINER;
CREATE ROLE pgbuser WITH LOGIN PASSWORD 'pgbouncer';
GRANT EXECUTE ON FUNCTION user_search(text) TO pgbuser;