Get lines of pg_hba.conf with trust method from user with minimal privileges I try on PostgreSQL 9.3 to get the lines of pg_hba.conf where the authentication method is trust from user with minimal privileges. I wrote a function, but it doesn't work:

create function public.get_pghba_with_trust() 
  returns TABLE (
    TYPE            text
  , DATABASE        text
  , PGUSER            text
  , ADDRESS         text
  , METHOD          text) 
as
$$ 
  SELECT * 
  FROM regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a) 
  WHERE a ~ '^[^#].*trust\s*$';
$$
language sql
security definer;


This returns an error:

ERROR:  return type mismatch in function declared to return record
DETAIL:  Final statement returns too few columns.
CONTEXT:  SQL function "get_pghba_with_trust"


Grant execute on function

grant execute on function get_pghba_with_trust() to redcheck;


Try other function

CREATE OR REPLACE FUNCTION get_pghba_with_trust() RETURNS text AS $$
DECLARE
  trust text;
BEGIN
  SELECT * INTO trust from regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a) WHERE a ~ '^[^#].*trust\s*$';
  IF NOT FOUND THEN
    RETURN 'not used';
  ELSE
    RETURN trust;
  END IF;
END;
$$ LANGUAGE plpgsql security definer;


Grant execute on function

grant execute on function get_pghba_with_trust() to redcheck;


Try run function get_pghba_with_trust

SELECT * FROM get_pghba_with_trust();


Error:

ERROR: must be superuser TO READ files 
CONTEXT: SQL STATEMENT "SELECT * from regexp_split_to_table(pg_read_file('pg_hba.conf'),'\n') t(a) WHERE a ~ '^[^#].*trust\s*$'" PL/pgSQL FUNCTION get_pghba_with_trust() line 5 AT SQL STATEMENT


How get lines of pg_hba.conf on PostgreSQL 9.3 with trust method from user with minimal privileges?
You are nearly there, just have to process the output from pg_read_file() as it returns each line of pg_hba.conf as a single value.  The following query will do the trick:

WITH file (line) AS (
    SELECT regexp_split_to_array(a, '\t+') 
      FROM regexp_split_to_table(pg_read_file('pg_hba.conf'), '\n') AS t(a)
     WHERE a ~ '^[^#].*trust\s*$'
)
SELECT line[1] AS type, 
       line[2] AS database, 
       line[3] AS username, 
       CASE WHEN array_length(line, 1) = 4 THEN NULL ELSE line[4] END AS range, 
       CASE WHEN array_length(line, 1) = 4 THEN line[4] ELSE line[5] END AS method
  FROM file;


This might not work in all cases that are possible in pg_hba.conf, though - you can tweak this query if you need to.

As you already noticed, pg_read_file() can be run only by superusers.  This is the very reason why you need SECURITY DEFINER on your function.  In order to make it work, you have to create the function as a superuser.  (Note that changing owner after a non-superuser created the function doesn't work.  If this is your case, drop the function first an create it anew, as a superuser this time.)