Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Overview of this book

Table of Contents (19 chapters)
PostgreSQL 9 Administration Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Hot logical backup of all tables in a tablespace


Sometimes, we may wish to make a dump of tables and data in a tablespace. Unfortunately, there isn't a simple command to do this, so we need to write some reusable scripts.

How to do it…

It is possible for a tablespace to contain objects from more than one database, so run the following query to see from which databases you need to dump:

SELECT datname FROM pg_database WHERE oid IN (SELECT pg_tablespace_databases(ts.oid)FROM pg_tablespace ts WHERE spcname = 'mytablespacename');

The following procedure allows you to dump all tables that reside on a given tablespace and within one database only:

  1. Create a file named onets.sql that contains the following SQL. This query extracts the list of tables in a tablespace:

    SELECT 'pg_dump '
    || array_to_string(
         array_agg('-t ' || n.nspname || '.' || t.relname)
       , ' ')
    || ' -F c ' || :'DBNAME'
    || ' > dumpfile_' || :'DBNAME' -- name of the output file
    FROM pg_class t
    JOIN pg_tablespace ts
      ON ts.oid...