Postgresql

From Elvanör's Technical Wiki
Jump to navigation Jump to search

Installation on Gentoo

  • The ebuilds that should be used are dev-db/postgresql-server-*, since they are newer and modularized instead of monolithic (old ebuilds are dev-db/postgresql).
  • There can be a problem with the postgres USE flag of php. It can pull in libpq which would block the modularized postgresql-server package. The solution is to build postgresql-server first (removing libpq if already installed), then rebuild PHP. This will probably be done automatically once postgresql-server is stabilized.
  • Similarly you need a version of psycopg (Python driver for Postgresql) that does not depend on libpq (2.0.10 is fine but unstable at the time of this writing).

Initial configuration

  • By default on Gentoo the socket file (on /var/run/postgresql directory) allows only reading (and writing) to the postgres user. This means that other users (like apache via mod_php) cannot connect to the DB via the UNIX socket. This implies that to make phppgadmin works (for instance) you have to explicitely write "localhost" as the server name in the configuration file.
  • This means also that if you want to give commands when using another user, never forget to add the -h localhost option (which will prevent the use of the local socket and will use TCP).

Concepts

Users and Roles

  • An user is just a Role that can login.
  • A DB or a table owner is actually a role (not an user, although see the previous remark).

Changing the ownership

  • REASSIGN OWNED won't work for reassigning stuff that belongs to the "cluster owner" (postgres role in general I think).
  • One way may be to export and reimport a DB as a different role. For this, use the --no-owner option of pg_dump:
sudo -u postgres pg_dump -O db_name > data.sql
  • Note that ALTER ... OWNER TO ... can probably help too.

Tools

  • psql is the command line interface for sending SQL commands to PostgreSQL.

Updating

  • You should dump the tables *before* you upgrade to another version (for example from 8.3 to 8.4). You can then restore the tables in the new version - but the initial dump should be done on the previous one.
sudo -u postgres pg_dump crm > crm.sql
psql crm < crm.sql # This is for restoring, you can also use: psql -U openerp crm < crm.sql

Management

  • Use the following command to copy a database to another one in the same server (the source DB must NOT be used):
create database newdb template olddb;

This is a Postgresql command, not standard SQL.

  • The following command (shell program) will create a database (using the openerp user):
createdb -U openerp new_db_name
  • You can drop via dropdb.