In PostgreSQL 9.0 and later, PL/pgSQL is pre-installed by default.
Version 9.0 also introduced
CREATE OR REPLACE LANGUAGE:
CREATE OR REPLACE LANGUAGE will either create a new language, or
replace an existing definition. If the language already exists, its
parameters are updated according to the values specified or taken from
To avoid raising an exception on older versions you can check the catalog table
pg_language. I quote the manual once more:
The system catalog pg_language [...] records information
about the currently installed languages.
SELECT EXISTS (
WHERE lanname = 'plpgsql');
Or use the client application
createlang, which has an option to check for existing languages:
createlang -l [connection parameters]