O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Encrypting sensitive data

This recipe shows how to encrypt data using the pgcrypto package.

Getting ready

Make sure you (or your database server) are in a country where encryption is not illegal—it still is in some countries.

Make sure pgcrypto is installed on your database host. On Ubuntu, it comes in package postgresql-contrib.

Install it into the database in which you want to use it:

psql mydb < /usr/share/postgresql/8.4/contrib/pgcrypto.sql

You also need to have GPG keys set up:

pguser@laptop:~$ gpg --gen-key

Answer some questions here, select key type "DSA and Elgamal", and enter an empty password.

Now export the keys:

pguser@laptop:~$ gpg -a --export "PostgreSQL User (test key for PG Cookbook) <pguser@somewhere.net>" > public.key pguser@laptop:~$ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required