Is there a way to perform a pg_dump and exclude the COMMENT ON for tables/views and columns ?
I use extensively the COMMENT ON command to describe all objects, and often include newlines in them for clearer descriptions, e.g.:
COMMENT ON TABLE mytable1 IS 'MAIN TABLE...
NOTES:
1. ...
2. ...
3. ...
';
However, since there are newlines in the dump as well, I cannot simply remove the comments with a grep -v 'COMMENT ON' command.
Any other way to quickly remove these COMMENT ON from the dump ?
pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even if the database is being used concurrently. pg_dump does not block other users accessing the database (readers or writers).
The only impact of pg_dump are the increased I/O load and the long running transaction it creates. The long transaction will keep autovacuum from reclaimimg dead tuples for the duration of the dump. Normally that is no big problem unless you have very high write activity in the database.
pg_dump compress option has the following description: Specify the compression level to use. Zero means no compression. For the custom archive format, this specifies compression of individual table-data segments, and the default is to compress at a moderate level.
Use the --no-comments
option.
Example:
$ pg_dump --no-comments [database] > dump.sql
References: https://www.postgresql.org/docs/12/app-pgdump.html
AFAIK, neither pg_dump
nor pg_restore
have options to remove COMMENT
s. But, if you use a binary dump format like:
$ pg_dump -Fc <your connection> -f /path/to/backup.dump
you could extract the TOC entry and edit it:
$ pg_restore -l -f /path/to/backup.toc /path/to/backup.dump
The above will extract a TOC file and save it at /path/to/backup.toc
, then you could find each line with COMMENT
entry and remove or comment it. If you don't use strange names on your objects, a simple sed
would solve the problem, to comment the lines with COMMENT
s you could do this (a semicolon starts a comment):
$ sed -i 's/^\(.* COMMENT .*\)/;\1/g' bar.toc
With this new TOC file, you can now use pg_restore
to restore your dump (with -L
option):
$ pg_restore -L /path/to/backup.toc -d <your database> /path/to/backup.dump
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With