CREATE OR REPLACE LANGUAGE
From PostgreSQL wiki
While there is no CREATE OR REPLACE for languages like there are for functions, you can simulate one for the common case where you want to add the pl/pgsql language to a database. Normally this will trigger an ERROR condition if the language is already installed:
$ psql -c "CREATE LANGUAGE plpgsql" ERROR: language "plpgsql" already exists
But the following snippet will add the language only if it doesn't already exist:
CREATE OR REPLACE FUNCTION make_plpgsql() RETURNS VOID LANGUAGE SQL AS $$ CREATE LANGUAGE plpgsql; $$; SELECT CASE WHEN EXISTS( SELECT 1 FROM pg_catalog.pg_language WHERE lanname='plpgsql' ) THEN NULL ELSE make_plpgsql() END; DROP FUNCTION make_plpgsql();
You can run this multiple times and it will never produce the error shown above. The DROP FUNCTION at the end is optional, if you want to re-use this snippet in other code you might keep the function around to be referenced later.
- Original code by David Fetter