82

I'd like to use pg_dump to backup postgres database content. I only want to ignore one specific table containing cached data of several hundred GB.

How could I achieve this with pg_dump?

4
  • 4
    Please read the manual there is a --exclude-table parameter: postgresql.org/docs/current/static/app-pgdump.html Commented Jul 28, 2015 at 8:38
  • 12
    Nothing wrong with a question like this. Just because something can be found in a manual somewhere, doesn't make it a bad question for SO. I've posted the answer as a community wiki. Commented Jul 28, 2015 at 8:51
  • @LondonRob it's funny that the correct answer is the first result in google if you search by the question subject. Commented Jul 28, 2015 at 8:54
  • 2
    This isn't really the place for this discussion, particularly because it's been had before and before and before Commented Jul 28, 2015 at 8:57

2 Answers 2

124

According to the docs, there is an option to --exclude-table which excludes tables from the dump by matching on a pattern (i.e. it allows wildcards):

-T table --exclude-table=table Do not dump any tables matching the table pattern. The pattern is interpreted according to the same rules as for -t. -T can be given more than once to exclude tables matching any of several patterns.

When both -t and -T are given, the behavior is to dump just the tables that match at least one -t switch but no -T switches. If -T appears without -t, then tables matching -T are excluded from what is otherwise a normal dump.

There are a few examples here.

Sign up to request clarification or add additional context in comments.

3 Comments

FWIW, --exclude-table was problematic for me due to some keys etc. that existed on the table but --exclude-table-data worked fine for me.
Note that if you use the table name it'll be ignored altogether. Apparently, you need to prepend the schema, as in --exclude-table-data="your_schema_here.your_table_here".
Also, if you have a capital letter in your table, don't forget to wrap it in ". For example public.File should be --exclude-table-data='public."File"'
-2

You can also add the same in the script

#!/bin/bash CKUPNUM=3 BACKUPDIR=/home/utrade/dbbackup DBBACKUP_FILENAME=Database_dump.sql TARFILE=Database_dump_$(date +%d%h%y).tgz #####Variables Set DBUSER=mutrade DBPASSWD=utrade123 DBNAME=mutradedb cd $BACKUPDIR export PGPASSWORD=$DBPASSWD /usr/pgsql-11/bin/pg_dump -f $DBBACKUP_FILENAME $DBNAME --exclude-table-data='appmaster.ohlc_*' -U $DBUSER tar czf $TARFILE $DBBACKUP_FILENAME rm -f $DBBACKUP_FILENAME #removing old/Extra backups backups_count=`ls -lrt | wc -l` if [[ $backups_count -gt $BACKUPNUM ]] then find $BACKUPDIR -mtime +30 -type f -delete fi 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.