Skip to main content

Generate function/table-wise backup/definition in PostgeSQL through a single script

Sometime we face problem to take backup or make (Definition) scripts of Functions or Tables separately for entire schema.
When we want to take backup separately for objects like function, table etc., here's need to generate object wise backup scripts separately.So for achieving this, go through below:
In this example I am using Database(DB)=postgres , User (U)=postgres, Schema=public  and OS=Centos7 x64
You can change name of your Database, User and Schema as per your requirement.

(A) Make separate Function scripts 
Step 1. Create an .sh(shell script) file and paste below commands to generate functions script by their functions name : 
Suppose I have created funcscript.sh
Step 2  Open it in vi editor :
                  vi  funcscript.sh
# Paste below text or change it as per your requirement. 
# Config:
DB=postgres
U=postgres
export PGPASSWORD=<your postgres user password>
# funcname searchpattern, if you want all funcs enter "":
P=""
# Directory to dump files without trailing slash:
DIR=~/psql_db_dump_dir/public
mkdir -p $DIR
publicfuncS="$(psql -d $DB -U $U -t -c "SELECT  nspname||'.'||proname
FROM    pg_catalog.pg_namespace n JOIN    pg_catalog.pg_proc p ON pronamespace = n.oid
WHERE   nspname = 'public' order by 1")"
for publicfunc in $publicfuncS; do
  echo backup $publicfunc ...
psql -d $DB -U $U -t -c "SELECT pg_get_functiondef('$publicfunc'::regproc)" > $DIR/$publicfunc.sql;
done;
echo done 
Step 3.  Give execute permission to this file funcscript.sh
chmod 700  funcscript.sh [Hint : Change it as per your requirement]
Step 4. Run this file
  ./funcscript.sh
 Step 5 . Go to /psql_db_dump_dir/public directory to check your dump scripts.

(B) Make separate Table wise scripts 
Step 1. Create an .sh(shell script) file and paste below commands to generate table script by their table name : 
Suppose I have created tablescript.sh
Step 2  Open it in vi editor :
                  vi  tablescript.sh
# Paste below text or change it as per your requirement.  
# Config:
DB=postgres
U=postgres
export PGPASSWORD=<your postgres user password>
# tablename search pattern, if you want all tables enter "":
P=""
# directory to dump files without trailing slash:
DIR=~/psql_db_dump_dir/public
mkdir -p $DIR
TABLES="$(psql -d $DB -U $U -t -c "SELECT table_schema||'.' ||table_name
FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema = 'public' ORDER BY table_name")"
for table in $TABLES; do
  echo backup $table ...
#  pg_dump $DB -U $U -w -t $table > $DIR/$table.sql;
pg_dump -U postgres -F plain --schema-only -t $table -w postgres > $DIR/$table.sql;
done;
echo done
[Hint : -F plain , it takes plain text backup , you can change format as per your requirement] 
Step 3.  Give execute permission to this file tablescript.sh
chmod 700  tablescript.sh [Hint :Change it as per your requirement]
Step 4. Run this file
  ./funcscript.sh
 Step 5 . Go to /psql_db_dump_dir/public directory to check your dump scripts.

Comments

  1. Great work Prakashji. Keep it up

    ReplyDelete
  2. Good post Prakash ji, bro helpful 👍👍👍

    ReplyDelete
  3. Thank you sir. We are expecting more from yours.

    ReplyDelete
  4. Great work.... Thank you for sharing your knowledge.....

    ReplyDelete

  5. Nice article sir.
    I have published xml tutorials if anybody want to learn XML.

    ReplyDelete
  6. A very well needed example and guidance, typically I have to backup all DB and structure if changes are made in only 1 schema. Thank you very much.
    Much appreciated.
    It would be of great help if along with postgres, MySQL tutorials are also mentioned.
    Thanks in advance !!!!

    ReplyDelete
  7. Thank you so much for providing information and highlighting many tools and utilities to look for vivid aspects and solve broader errors.

    SSIS Postgresql Write

    ReplyDelete

Post a Comment

Popular posts from this blog

SSL configuration in PostgreSQL for PgAdmin

SSL configuration in PostgreSQL for PgAdmin This document describes how to set up SSL to enable ssl connections from PgAdmin on some client machine to postgresql on a server machine. In my example, I am using Centos 7 for PostgreSql-9.5 server and Windows for PgAdmin.The assumption is that Postgresql (compiled with ssl support) and openssl are already installed on the server (Linux Centos7). PgAdmin is already installed on the client (either Windows or Linux). On the server, three certificates are required in the data directory. root.crt (trusted root certificate) server.crt (server certificate) server.key (private key)  On CentOS7 default Data directory is / var/lib/pgsql/9.5/data/ : Now, connect your system with user root. Step 1 Su – Go to data directory [root@localhost ~]# cd /var/lib/pgsql/9.5/data/ Out put [root@ localhost data]# Step 2 : Generate a private key (you must provide a passphrase). [root@ localhost data]# ope...

Partition and Subpartition in PostgreSQL12

Partitioning refers to splitting what is logically one large table into smaller physical pieces. Partitioning can provide several benefits: • Query performance can be improved dramatically in certain situations, particularly when most of the heavily accessed rows of the table are in a single partition or a small number of partitions.  • When queries or updates access a large percentage of a single partition, performance can be improved by taking advantage of sequential scan of that partition instead of using an index and random access reads scattered across the whole table. • Bulk loads and deletes can be accomplished by adding or removing partitions, if that requirement is planned into the partitioning design. Doing ALTER TABLE DETACH PARTITION or dropping an individual partition using DROP TABLE is far faster than a bulk operation. These commands also entirely avoid the VACUUM overhead caused by a bulk DELETE. • Seldom-used data can be migrated to cheaper and slower...

PostgreSQL Foreign Data Wrapper for Oracle on Windows OS

The foreign data wrapper is responsible for fetching data from the remote data source and returning it to the PostgreSQL executor. If wrapper must support in updating foreign tables. In this post, I'll demonstrate, how you may connect PostgreSQL database to Oracle database using oracle_fdw . Before going ahead kindly ensure that PostgreSQL is installed in your system . Let’s begin: Step-1 Software’s used in demo - Windows, PostgreSQL 9.5, 9.6   instantclient-basic-win-x86-64-11.2.0.1.0 instantclient-jdbc-win-x86-64-11.2.0.1.0   instantclient-odbc-win-x86-64-11.2.0.1.0 instantclient-sqlplus-win-x86-64-11.2.0.1.0 & Oracle_FDW as per version ( http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html   , https://pgxn.org/dist/oracle_fdw/ or version wise)   Download all the files mentioned above, in a single folder and unzip entire files to this single folder. Suppose, we create a folder Oracle in c: drive ...