Gentoo - Postgres installation

By: John McFarlane <john.mcfarlane@rockfloat.com>
Last updated: 07/20/2005 @ 18:45

Abstract:
This document will go thru a step by step installation of PostgreSQL on Gentoo Linux.



1. Install postgresql via portage

In order to use postgres, we need the actual server installed. This will also install some other programs that are necessary such as psql.

root# emerge --sync
root# emerge -a postgresql
        
Portage will on your behalf add an operating system account of postgres which has a home directory of /var/lib/postgresql

You will however need to add an entry into /etc/shadow for postgres:


root# echo "postgres::::::::" >> /etc/shadow
        	
Else you'll get this later on:

su: Authentication service cannot retrieve authentication info.

I'm finished with this step

2. Make the database storage locations


root# mkdir -p /path/db/data
root# mkdir -p /path/db/logs
root# chown -R postgres:postgres /path/db
        
If you don't want to specify the sorage location, you can just take the default which is located: /var/lib/postgresql/data. If you want to use this location, just use it below as your data directory and use /var/lib/postgresql/postgres.log as the log.
I'm finished with this step

3. Edit /etc/conf.d/postgresql setting the following


PGDATA=/path/db/data
PGLOG=/path/db/logs/postgres.log
PGUSER=postgres

# Uncomment this if you want to support TCP/IP connections
# PGOPTS="-N 1024 -B 2048 -i"
        
I'm finished with this step

4. Initialize the database


root# su - postgres -c 'export PGDATA=/path/db/data && initdb'
        
I'm finished with this step

5. Startup the database for the first time :)


root# /etc/init.d/postgresql start
        
I'm finished with this step

6. Become the postgres user


su - postgres
        
I'm finished with this step

7. Setup our main user (reduced previlages)


psql# createuser rockfloat
OUTPUT: Shall the new user be allowed to create databases? (y/n) n
OUTPUT: Shall the new user be allowed to create more new users? (y/n) n
psql# psql template1
template1=# alter user rockfloat with password 'foobar';
        OUTPUT: ALTER USER
template1=# \q
--> create the live database        
psql# createdb rockfloat
        OUTPUT: CREATE DATABASE
--> populate it        
psql# psql -e rockfloat < rockfloat.sql

--> create the development db        
psql# createdb rockfloat_monkey
        OUTPUT: CREATE DATABASE
--> populate it        
psql# psql -e rockfloat_monkey < rockfloat.sql

psql# exit
        
I'm finished with this step

8. Alternately set things up as a normal user

You can also use the client tools from a normal shell account:

user# createdb -E UTF-8 -O postgres -U postgres rockfloat
        
I'm finished with this step

9. Make sure postgres starts on bootup


root# rc-update add postgresql default
        
I'm finished with this step

10. If you ever need to backup a database for maybe an upgrade or something use the following syntax:


root# su - postgres -c 'pg_dump -c foodatabase > backup.sql'

# Or without using sudo:
user# pg_dump -U postgres foodatabase > backup.sql
        
I'm finished with this step

11. To restore from either of these backups, you would do:


root# su - postgres -c 'psql -e foodatabase < backup.sql'

# Or without using sudo:
user# psql -e foodatabase postgres < backup.sql
        
I'm finished with this step

12. To backup one table in a particular table


root# su - postgres -c 'pg_dump -c -t footable foodatabase > tablebackup.sql'

# Or without using sudo:
user# pg_dump -U postgres -c -t footable foodatabase > tablebackup.sql
        
I'm finished with this step

13. I haven't tested this, but I assume you would then restore the table like this:


root# su - postgres -c 'psql -e foodatabase -t footable < tablebackup.sql'

# Or without using sudo:
user# psql -e -t footable foodatabase postgres < tablebackup.sql
        
I'm finished with this step

Changelog: Date Description
12/15/2003 @ 21:00 Initial creation
04/04/2006 @ 01:20 Added some more backup/restore examples
07/20/2005 @ 18:45 Updated to Gentoo-2006.1 and Postgresql-8.0.8

This document was originally created on 12/15/2003


Conventions and tips for this howto document:
  1. Databases backups are referred to as foobar.sql

Disclaimer:
This page is not endorsed by gentoo.org or any other cool cats. Any information provided in this document is to be used at your own risk.