This section is from the "Practical PostgreSQL" book, by John Worsley and Joshua Drake. Also available from Amazon: Practical PostgreSQL.
Before you can use the PL/pgSQL language, you will need to add it to your database. Use these steps to add the PL/pgSQL language to any existing database:
The first step to getting PL/pgSQL up and running is to log into the PostgreSQL database as a PostgreSQL superuser. For simplicity sake, we are going to use the standard PostgreSQL superuser postgres. As a reminder, below are the command required to login using a Unix/Linux based system to the postgres user.
su - postgres
Adding procedural languages to PostgreSQL is done by using the createlang command.Use the creatlang command. When the creatdb command is used, it utilizes the template1 database to create the database.
Remember template1 | |
---|---|
If you use the creatlang command on the template1 database, then every subsequent database created will have the PL/pgSQL language automatically loaded. |
To add PL/pgSQL to the booktown database use the following command:
createlang plpgsql booktownThe createlang program will return you to a shell prompt upon successfull execution.
You are now ready to log into your database and use the PL/pgSQL language to define functions for PostgreSQL. Please refer to the subsequent sections on ways to define functions, blocks, and variables using PL/pgSQL.
 
Continue to: