User defined variables in PostgreSQL

Postgres does not normally use variables in plain SQL. But you can do that, too: SET foo.test=”SELECT bar FROM baz”; SELECT current_setting(‘foo.test’); Read about Customized Options in the manual. In PostgreSQL 9.1 or earlier you needed to declare custom_variable_classes before you could use that. However, You cannot EXECUTE dynamic SQL without a PL (procedural language). … Read more

Can I make a plpgsql function return an integer without using a variable?

Yes you can. There are a number of ways. 1) RETURN (SELECT …) CREATE OR REPLACE FUNCTION get_1(_param_id integer) RETURNS integer LANGUAGE plpgsql AS $func$ BEGIN RETURN _param_id; — Or: — RETURN (SELECT col1 FROM tbl WHERE id = _param_id); END $func$; 2) Use an OUT or INOUT parameter CREATE OR REPLACE FUNCTION get_2(_param_id integer, … Read more

Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections [duplicate]

The error you quote has nothing to do with pg_hba.conf; it’s failing to connect, not failing to authorize the connection. Do what the error message says: Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections You haven’t shown the command that produces the error. Assuming you’re connecting on … Read more

Refresh a materialized view automatically using a rule or notify

You should refresh the view in triggers after insert/update/delete/truncate for each statement on table1 and table2. create or replace function refresh_mat_view() returns trigger language plpgsql as $$ begin refresh materialized view mat_view; return null; end $$; create trigger refresh_mat_view after insert or update or delete or truncate on table1 for each statement execute procedure refresh_mat_view(); … Read more

Grant all on a specific schema in the db to a group role in PostgreSQL

You found the shorthand to set privileges for all existing tables in the given schema. The manual clarifies: (but note that ALL TABLES is considered to include views and foreign tables). Bold emphasis mine. serial columns are implemented with nextval() on a sequence as column default and, quoting the manual: For sequences, this privilege allows … Read more