Postgresql: How to apply pg_dump --exclude-table-data patch?

8k views Asked by At

I have a production server (ec2) with a Django App and Postgresql Database. I make a DB backup everynight with pg_dump which backs up the tables data.

sudo -u postgres pg_dump --column-inserts --data-only mydb > mybackup.sql

Postgres provides the possibility of ignoring some tables to be backed up (tables schema + data: --exclude-table=TABLE)

However I have some tables, that I would like backup their schema but not their data, I just want to dump the table with empty data, the old data are not important at all, but they make the backup file huge if dumped.

There is a patch "exclude-table-data=TABLE" that allows a user to have pg_dump exclude data but not DDL for a table. One use case for this is a very large table that changes infrequently, and for which dumping data frequently would be wasteful and unnecessary.

I would like to know how to apply this patch without losing anything on my database on my production server.

2

There are 2 answers

0
Yahya Yahyaoui On BEST ANSWER

I found a better solution, upgrade PostgreSQL to a version 9.2 or higher, and I found a safe way to do so: (Upgrade PostgreSQL 9.1 to 9.3 on Ubuntu 12.04)

echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3 postgresql-contrib-9.3 -y
sudo su - postgres -c "psql template1 -p 5433 -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
sudo su - postgres -c "psql template1 -p 5433 -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
sudo su - postgres -c "service postgresql stop"
sudo su - postgres -c '/usr/lib/postgresql/9.3/bin/pg_upgrade -b /usr/lib/postgresql/9.1/bin -B /usr/lib/postgresql/9.3/bin -d /var/lib/postgresql/9.1/main/ -D /var/lib/postgresql/9.3/main/ -O "-c config_file=/etc/postgresql/9.3/main/postgresql.conf" -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"'
sudo apt-get remove postgresql-9.1 -y
sudo sed -i "s:5433:5432:g" /etc/postgresql/9.3/main/postgresql.conf
sudo service postgresql restart 

Note: This will install both PostgreSQL 9.3 and 9.4, so whether remove PostgreSQL 9.4 when finished or change the above code to upgrade to version 9.4.

Reference.

Later I realized that, since I'm using --data-only, --exclude-table and --exclude-table-data would have the same effect since I'm ignoring the schema !

2
Renzo On

exclude-table-data is now a regular option of pg_dump from PostgreSQL 9.2+, so if your server is a recent version, it is already available (see documentation page: http://www.postgresql.org/docs/9.2/static/app-pgdump.html)