pg_dumpall excluding some tables - postgresql

I want to get a full backup of postgres 9.6. Including the users and permissions. However I want to exclude some tables. In pg_dump there is an option for excluding some tables (-T). However in pg_dumpall there are no such options.
Is there a way for getting a backup like this in a single command? Or should I get pg_dumpall (without tables) and pg_dump with -T? However in the second scenario these two dumps are not completely synchronised.

You'll have to use pg_dumpall -g and pg_dump -T.
True, the dumps will not share a single snapshot, but unless you add, modify or delete users and tablespaces very frequently, that should not be a problem in practice.

Related

Dump all DATABASE with specifi prefixe with pg_dump

Im currently using the tool pg_dump to backup my database.
I have a lot of database. I don't know their full name but I know that all DATABASES have a well defined prefix.
I would like to automate the process of backup all my DATABASES, however, i have not found a way to specify pg_dump to dump multiple DATABASE that have the same prefix.
I said database and not table nor schema, because I tried the commands in the pgsql doc which gives the options -n for schemas and -t for tables.
But that's not what I want to do, I want to save all my databases with a defined prefix.
Any help in this matter would be greatly appreciated

How to exclude a particular table in postgres backup using pgbackrest?

Trying to exclude a particular table alone during postgres backup. Is there something similar in pgbackrest like pg_dump
pg_dump --exclude-table-data=ex_table demodb
I have not found such option in pbBackRest and that is expected because it's a physical backup tool and not a logical backup tool like pg_dump.

How can I copy my whole postgres database to another postgres one?

I have two postgres databases on the same server, a live one and another one which I use as a test database.
Periodically I need to copy my live database (both structure of the tables and their data) into the test one, but everything I've found online only copies table by table. Since I often create new tables in my live db I can't do this, otherwise I'd have to update the job every time.
Anybody knows how can I pull the whole live postgres db into the test postgres one?
Thanks!
You want to use pg_dump and pg_restore.
Example usage would be:
$ pg_dump -Fc <database_name> > dumpfile
$ pg_restore <another_database_name> < dumpfile
If you want to do this for all database you might want to consider pg_dumpall
A bit more can be found in https://www.postgresql.org/docs/current/backup-dump.html

Restoring the data from pg_dump doesn't overwrite the data but it appends the data to the original database

I am taking the dump of postgres database using "pg_dump database_name > backup.sql". Later on I am doing some modifications in the original database(database_name) and then I am restoring the data from the backup file(backup.sql). But the result is that, the database doesn't gets restored to the original state, instead it adds the original data to the modified data(modified + original).I just want it to restore to the original state, shall i delete all the data from the database before restoring it from the backup file, since it gives the original state of the database. Or is there any other way to do this?
The default format fo pg_dump is plain, so it creates a COPY statement. Hence when you psql backup.sql you just run those copy over existing data. To rewrite data, you should either drop tables first or pg_dump -F c and pg_restore -c.
Warning - in both cases it will destroy old data (this seems what you want though)
-c
--clean Clean (drop) database objects before recreating them. (Unless --if-exists is used, this might generate some harmless error messages, if any objects were not present in the destination database.)
As #Craig Ringer suggests, drop/recreate from backup would be much easier and cleaner. To drop database you run DROP DATABASE au - note that there should be no connected users to success. Then you have to create db: CREATE DATABASE au and run psql -f backup.sql -d au
Take the dump with -c option: pg_dump -c database_name > backup.sql. See pg_dump docs.

Can I restore just one schema from a pg_dump of the entire database?

I have backups of my postgres database - the entire database instance in a single nightly backup. Is it possible to restore just one of the database from within that backup? Or if I want to access individual databases (for migration or restoring), do I need to change my database backup scheme to do individual dumps?
You can access individual databases from a full database cluster dump in text format (pg_dumpall) by using some text processing like this:
awk '/^\\connect database_name/ {flag=1;print;next}
/^\\connect/ {flag=0}
flag { print }' \
< all_databases.sql \
> database_name.sql
This would get from pg_dumpall file everything between "\connect database_name" and next "\connect". But it is not very efficient.
But I'd recommend dumping every database separately like this:
# Dumping global data (for example roles)
pg_dumpall -g > /var/lib/pgsql/backups/globals.sql
#Dumping indidual databases in tar (uncompressed binary) format
for dbname in
`
psql -qXtc "
select datname from pg_catalog.pg_database
where datname<>'template0'" template1
`
do
pg_dump -b -F t "$dbname" > "/var/lib/pgsql/backups/$dbname.dump"
done
I'm assuming you mean "Can I restore just one database from a pg_dumpall of the entire database cluster?"
Yes, if your backup is "an archive created by pg_dump in one of the non-plain-text formats." - use the "--schema=" option to pg_restore.
On the other hand I'm not sure you're using the correct terminology here - you refer to a DB cluster as "entire database instance"; in the explanation you ask about "database" but in the title you wrote "schema", etc.
Edit: Now, after some deliberation, I believe you have a cluster backup, created with "pg_dumpall". "pg_dumpall" creates only plain-text (SQL-commands) backups. In that case it's not possible to restore only one database (or only one schema from a database).
Then yes, you need to change your backup procedure to backup individual databases using non-plain-text format (--format=custom is the recommended one).
Actually the backup procedure I use on my DB servers does just that.