Department of Computing | Imperial College London |
PostgreSQL Database |
The PostgreSQL website has a variety of documentation including a book called PostgreSQL: Introduction and Concepts.
See also
/usr/share/doc/postgresql-docs-7.3.4/index.html
Our teaching Postgres database server runs on a machine called db on port 5432. It provides access by all DoC users, and additional Postgres specific usernames, to each of many databases -- each one a set of tables etc. Within our teaching Postgres database server, there are three categories of database:
The Postgres clients ( psql, pg_dump etc) can either ask for some/all of these settings when you run them, or take them from a set of environment variables and a data file. For convenience, we strongly recommend that you set these up once and for all by creating two files - first, ~/.pg with the following contents:
setenv PGHOST db setenv PGPORT 5432 setenv PGUSER {your_user_name} setenv PGDATABASE {your_database_name} unsetenv PGPASSWORD
Second, create ~/.pgpass with the contents:
db:*:*:{your_user_name}:{your_postgres_password}
Note: or course, you must replace the {your_user_name}, {your_database_name} and {your_postgres_password} with your postgres username, database name and password! Assuming your database name and username are the same as your Unix username, you could write this as:
setenv PGUSER $user setenv PGDATABASE $user
Then you must ensure that your ~/.pgpass file is not accessible by others - to do this, simply type the following Unix command:
chmod go= ~/.pgpass
Note: Postgres used to have an environment variable called PGPASSWORD, this still works but is now deprecated. You will notice that the ~/.pg shown above includes unsetenv PGPASSWORD to make sure that a spurious PGPASSWORD setting doesn't override your ~/.pgpass password. A particular circumstance where this often occurs is when, in order to make Postgres-based labs easier, the Lab Organisers set up some default Postgres settings for you - usually:
setenv PGUSER lab setenv PGDATABASE films setenv PGPASSWORD labThis is perfectly sensible, but when you come to use your own database you must make sure you are not using a mixture of these settings and the ones shown above. So make sure unsetenv PGPASSWORD is included in the ~/.pg file as shown above and it'll work fine.
Once you've set up these files, you should source ~/.pg to start using the database. If you want this to happen automatically on login, add source ~/.pg to the end of your ~/.cshrc file. This will then override the films/lab/lab settings completely - so of course you would have to change them back to do some lab exercises.
Having sourced the ~/.pg file and created the ~/.pgpass file, you can just run the Postgres interactive SQL program psql to access your database, or psql database to access another database. If you have not provided Postgres with your Postgres password by any means, you will be prompted for a password. This usually means your setup files are broken - so fix them and try again.
use DBI; $dbh = DBI->connect("dbi:Pg:dbname=$database;host=db;port=5432", $username, $password);
require 'postgres' db = PGconn.connect('db',5432,nil,nil,database,user,password)
© CSG / 2004 |