Postgresql plpg syntax

From wikinotes

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