Can I backup/restore a database in a sql script?

Lists: pgsql-adminpgsql-general
From: "Joost Kraaijeveld" <J(dot)Kraaijeveld(at)Askesis(dot)nl>
To: <pgsql-general(at)postgresql(dot)org>
Cc: <pgsql-admin(at)postgresql(dot)org>
Subject: Can I backup/restore a database in a sql script?
Date: 2007-06-22 07:59:24
Message-ID: A3D1526C98B7C1409A687E0943EAC41060600F@obelix.askesis.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

Hi,

I want to write a sql script in which I backup a database and restore a new (altered) version of that database. Is that possible? If so , can anyone give me an example of how to do that?

I can run it from any command prompt (psql -U postgres template1 < my_db.backup) but I would like it to run from psql (which should give me 1 script for all platforms I must do this: Windows, FreeBSD and Debian)

TIA

Joost


From: Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: Can I backup/restore a database in a sql script?
Date: 2007-06-22 08:13:09
Message-ID: 200706221113.10105.achill@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

Στις Παρασκευή 22 Ιούνιος 2007 10:59, ο/η Joost Kraaijeveld έγραψε:
> Hi,
>
> I want to write a sql script in which I backup a database and restore a new
> (altered) version of that database. Is that possible? If so , can anyone
> give me an example of how to do that?
>
> I can run it from any command prompt (psql -U postgres template1 <
> my_db.backup) but I would like it to run from psql (which should give me
> 1 script for all platforms I must do this: Windows, FreeBSD and Debian)
>

With "\!" you can execute shell commands.
So you could probably
\c template1
\! pg_dump your_db > your_db.sql
\! some_processing your_db.sql > your_db_altered.sql
DROP DATABSE your_db
CREATE DATABSE your_db
\i your_db_altered.sql

I dont know if the \! escape works for windows, and in any case pg_dump,
some_processing programs must be in the PATH for Unix/Windows

Debian/FreeBSD behaviour is exactly the same, it is on windows that you should
try to make it work.

> TIA
>
> Joost
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq

--
Achilleas Mantzios


From: Richard Huxton <dev(at)archonet(dot)com>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-admin(at)postgresql(dot)org
Subject: Re: [GENERAL] Can I backup/restore a database in a sql script?
Date: 2007-06-22 09:09:39
Message-ID: 467B91D3.60300@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

Joost Kraaijeveld wrote:
> Hi,
>
> I want to write a sql script in which I backup a database and restore
> a new (altered) version of that database. Is that possible? If so ,
> can anyone give me an example of how to do that?
>
> I can run it from any command prompt (psql -U postgres template1 <
> my_db.backup) but I would like it to run from psql (which should
> give me 1 script for all platforms I must do this: Windows, FreeBSD
> and Debian)

You can restore using just psql (because pg_dump can produce an sql file
to be processed). I don't know of any way to backup a database that
doesn't use pg_dump.

--
Richard Huxton
Archonet Ltd


From: Erik Jones <erik(at)myemma(dot)com>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>, pgsql-admin(at)postgresql(dot)org
Subject: Re: [GENERAL] Can I backup/restore a database in a sql script?
Date: 2007-06-22 17:24:50
Message-ID: 56B5B421-07A1-467E-B236-6FEEAFEA544C@myemma.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

On Jun 22, 2007, at 4:09 AM, Richard Huxton wrote:

> Joost Kraaijeveld wrote:
>> Hi,
>> I want to write a sql script in which I backup a database and restore
>> a new (altered) version of that database. Is that possible? If so ,
>> can anyone give me an example of how to do that?
>> I can run it from any command prompt (psql -U postgres template1 <
>> my_db.backup) but I would like it to run from psql (which should
>> give me 1 script for all platforms I must do this: Windows, FreeBSD
>> and Debian)
>
> You can restore using just psql (because pg_dump can produce an sql
> file to be processed). I don't know of any way to backup a database
> that doesn't use pg_dump.
>

You could script a PITR backup and recovery procedure but I doubt any
given single solution would be portable between Windows, Debian, and
FreeBSD given the differences in filesystems and filesystem tools
although I suppose with a lot of careful work it may be possible.
See the chapter on Backup and Restore in the manual for the details
of how PITR works (http://www.postgresql.org/docs/8.2/interactive/
backup.html).

Erik Jones

Software Developer | Emma®
erik(at)myemma(dot)com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com