简体   繁体   中英

How do I restore a Postgres database from within a SQL query?

When in development, I need to clean down my system by restoring a database backup and then running SQL against it. Is there a way to do this:

restore schema dbo 'c:/data/mydatabase.backup';
update dbo."MyTable" set "ColumnA" = 1;

Unlike other questions, this is not for doing via the command line but in a query instead.

Postgres v9.5 with pgAdminIII.

There is no support to restore a dump from within a SQL statement in Postgres.


Some possible (ugly) workarounds:

If your dump is a SQL script you could theoretically run that script from wherever you would run that (hypothetical) restore statement - but that would require parsing the script. And you would need to generate the dump using the --inserts option as otherwise the script contains psql specific statements.

Another workaround could be to create a stored function in a un-trusted language that is able to run operating system commands. The actual restore would still be done by the command line utility, but you could initiate that from within SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM