Postgresql plpg syntax: Difference between revisions

From wikinotes
(Created page with "In addition to sql's one-transaction-per-statement, postgres enables you to use pgsql or plsql script. I have barely used this, and only ever to compensate for items like role...")
 
(No difference)

Latest revision as of 13:05, 15 September 2019

In addition to sql's one-transaction-per-statement, postgres enables you to use pgsql or plsql script. I have barely used this, and only ever to compensate for items like roles which cannot use IF NOT EXISTS.

\set ON_ERROR_STOP on  -- postgres does not halt on errors by default

DO
$$
BEGIN
    IF NOT EXISTS(SELECT * FROM pg_roles WHERE rolname = 'readonly') THEN
        CREATE ROLE readonly;
    END IF;
END;
$$

LANGUAGE plpgsql;

This can then be run from the commandline:

psql -f yourfile.pgsql