Friday 17 August 2007

Migrating a postgreSQL database as a plain-text file

There was this postgreSQL database on a remote machine which I had to reconstruct on my local machine, the database structure and content was dumped as a .sql and I had to reconstruct from the .sql. I tried running the query file from pgAdmin's query tool but to no avail, it simply didn't work; no effect no error messages. I later found a solution which I describe below:

A database could be dumped in 2 ways; as an archive which can be restored from pgAdmin using the pg_restore tool or as a plain-text file like the .sql I had. To restore from plain-text files, one has to use psql command-line tool. For those of you using WinXP pro sp2 and installed your postgreSQL like me, here's how to do it:

Go to Start menu> All programs> PostgreSQl and select psql to 'postgres'

It starts up a command-line window already connected to a default database. If thats the database you want to run the queries on, fine. Else you would have to change the database by typing:

\c name_of_database

then you will have to change directory to the directory where your .sql resides using

\cd name_of_directory
all \ in the directory name should be typed as / cos psql is UNIX-based and would confuse windows' \ for commands. also try and ensure that file and directory names conform to 8.3 filename format. Mine did; I don't know if it will work if yours don't

finally execute the commands by typing

\i name_of_file

if you have any problems with the above procedure, let me know.