PostgreSQL is a first-rate, enterprise-worthy open source RDBMS (relational database management system) that compares very favourably to high-priced closed source commercial databases.

Databases are complex, tricky beasts full of pitfalls. In this crash course, we'll get a new PostgreSQL database up and running with elegant ease, and learn important fundamentals.

If you're a database novice, then give yourself plenty of time to learn your way around. PostgreSQL is a great database for beginners because it's well documented and aims to adhere to standards. Even better, everything is discoverable, nothing is hidden, not even the source code, so you can develop as complete an understanding of it as you want.

Planning

The most important part of administering any database is preparation, in planning and design and in learning best practices. A good requirements analysis will help you decide what data to store, how to organise it and what business rules to incorporate. You'll need to figure out where your business logic goes: in the database, in middleware or applications?

You may not have the luxury of a clean, fresh new installation, but must instead grapple with a migration from a different database. These are giant topics for another day. Fortunately there are plenty of good resources online, starting with the excellent PostgreSQL manuals and Wiki.

Installation

We'll use three things in this crash course: PostgreSQL, its built-in interactive command shell psql, and the excellent pgAdmin3 graphical administration and development tool. Linux users will find PostgreSQL and pgAdmin3 in the repositories of their favourite Linux distributions, and there are downloads on PostgreSQL.org for Linux, FreeBSD, Mac OS X, Solaris and Windows.

There are one-click installers for OS X and Windows, and they include pgAdmin3. Any of these operating systems are fine for testing and learning. For production use, I recommend a Linux or Unix server, because they're reliable, efficient and secure.

Linux and FreeBSD split PostgreSQL into multiple packages. You want both the server and client. For example, on Debian the metapackage postgresql installs all of these packages:

# apt-get install postgresql

postgresql postgresql-9.0 postgresql-client-9.0

postgresql-client-common postgresql-common

See the detailed installation guides on the PostgresSQL wiki for more information for all platforms.

The downloads page also includes some live CDs which make it dead easy to set up a test server. Simply boot the CD and go to work. For this article, I used a Debian Wheezy (Testing) system running PostgreSQL 9.0.4, the current stable release.

Creating and destroying a new PostgreSQL database

You'll have to jump through a number of startup and user account hurdles to get started. First, verify that your PostgreSQL server has started by opening psql, its interactive command shell:

$ psql

psql: could not connect to server: No such file or directory

Oops, PostgreSQL is not running. Go back to the appropriate installation guide to see how to start it on your system. On Debian, it starts automatically after installation, so the above command produces the following result:

carla@xena:~$ psql

psql: FATAL: role "carla" does not exist

Well, excuse me all to heck! But this really isn't a big deal, because PostgreSQL creates a default postgres superuser with no password. So you have to change to this user, and then create a new user account that you will use to administer the database. On Linux and Unix you need to gain rootly powers, like this:

carla@xena:~$ su

root@xena:/home/carla# su postgres

postgres@xena:/home/carla$

There, now we can get some real work done! Let's create a carla superuser for PostgreSQL:

postgres@xena:/home/carla$ createuser carla

could not change directory to "/home/carla"

Shall the new role be a superuser? (y/n) y

These PostgreSQL roles, postgres and carla, are PostgreSQL user accounts that are independent of system accounts. A role can be a single user or a group of users. Roles can own database objects, such as tables, and can assign privileges to access those tables to other roles. Use the dropuser command to delete a role:

$ dropuser carla

Now, let's create a brand new database:

$ createdb testdb

No news is good news. If this is successful, there will be no feedback. You'll see a message only if something went wrong. Now let's destroy our new database:

$ dropdb testdb

Again, silence equals success.