Postgresql: backup all table structures but only a few data table - postgresql

I have a database with some tables for the application settings, lists like users, departments, cities. I want the structure and the data for those tables. So if i get a new user the backup will save it.
But also have some data for historic and calculated data, that data came from another sources and only work for some time and then expire, so backup that data will be a waste. But will need have the structure so the restore will create the tables need it for the application.
right now I'm using this command but this save all table and all data.
pg_dump -U "postgres" -h "local" -p "5432"
-d dbName -F c -b -v -f c:\uti\backup.dmp
I have 2 additional questions regarding pg_dump.
A) docs say option -b is for blob data. I have very big tables, but i guess this options is for only tables with a BLOB field, so shouldn't make any difference in my backup because i don't have those fields ?.
B) I see pg_dump options are for tables and schemas. How you specify if want save the functions code?

Exclude the tables you do not want to backup
pg_dump -U "postgres" -h "local" -p "5432"
-d dbName -F c -b -v -f c:\uti\backup.dmp
--exclude-table-data '*.table_name_pattern_*'
--exclude-table-data 'some_schema.another_*_pattern_*'
The function creation code is part of the schema.

Clodoaldo Neto's is the way to go. However, I witnessed a strange behavior when using pg_dump with capital letters. The issue is also described here
So in my case the table to ignore was named ChangeHistory and the trick to ignore it was wildcarding capital letters as below
pg_dump [omitted for brievity] --exclude-table "*hange*istory"

Related

How To Restore Specific Schema From Dump file in PostgreSQL?

I have a dump file (size around 5 GB) which is taken via this command:
pg_dump -U postgres -p 5440 MYPRODDB > MYPRODDB_2022.dmp
The database consists multiple schemas (let's say Schema A,B,C and D) but i need to restore only one schema (schema A).
How can i achieve that? The command below didn't work and gave error:
pg_restore -U postgres -d MYPRODDB -n A -p 5440 < MYPRODDB_2022.dmp
pgrestore: error: input file appears to be a text format dump. please
use psql.
You cannot do that with a plain format dump. That's one of the reasons why you always use a different format unless you need an SQL script.
If you want to stick with a plain text dump:
pg_dump -U postgres -p 5440 -n A MYPRODDB > MYPRODDB_2022.dmp
psql -U postgres -d MYPRODDB -p 5440 -f MYPRODDB_2022.dmp
Though dumping back over the same database as above will throw errors unless you use --clean or its short form -c to create commands to drop existing objects before restoring them:
-c
--clean
Output commands to clean (drop) database objects prior to outputting the commands for creating them. (Unless --if-exists is also specified, restore might generate some harmless error messages, if any objects were not present in the destination database.)
This option is ignored when emitting an archive (non-text) output file. For the archive formats, you can specify the option when you call pg_restore.
Probably also a good idea to throw in --if-exists:
--if-exists
Use conditional commands (i.e., add an IF EXISTS clause) when cleaning database objects. This option is not valid unless --clean is also specified.

restoring table with pg_restore does not include primary key or indexes

So I made a backup of a table using pg_dump:
pg_dump -U bob -F c -d commerce -t orders > orders.dump
This table had several listed indexes such as a primary key
However when I restore this table into a development database on another system using pg_restore:
pg_restore -U bob -d commerce -t orders > orders.dump
No primary key or indexes are listed
What am I doing wrong?
You are doing nothing wrong, unfortunately pg_restore -t restores only the table, nothing else, regardless of how you created the dump and what is inside the dump itself.
This has been somehow clarified in V12 PostgreSQL docs, that states:
This flag does not behave identically to the -t flag of pg_dump. There is not currently any provision for wild-card matching in pg_restore, nor can you include a schema name within its -t. And, while pg_dump's -t flag will also dump subsidiary objects (such as indexes) of the selected table(s), pg_restore's -t flag does not include such subsidiary objects.
the only way to make sure that restoring a table will carry all the indexes is to address them by name, something like:
pg_restore -U bob -d commerce -t orders -I index1 -I index2 -I index3 > orders.dump

Export Heroku Postgres database, but exclude a table

I want to get an export of my Heroku application's Postgres database, however I want to exclude one table. Is this possible?
Here is the command I use to export my entire Postgres database:
$ PGUSER=my_username PGPASSWORD=my_password heroku pg:pull DATABASE_URL my-application-name`
Maybe there is a way to exclude one table, or specify a list of tables to include?
In normal pg dump command you can specify the tables to include with -t option and exclude tables with -T option.
Can you try this :
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -T *table you want to exclude* -h localhost -U myuser mydb > mydb.dump
Here is the document copied from postgreql official document.
-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.
here is link for your reference
http://www.postgresql.org/docs/9.1/static/app-pgdump.html

Postgres: Copying particular tables from remote db, interrelated with foreign key relationships

I have a Postgres RDS instance for one of my apps.
I need to copy 3 tables from it to a similar clone of the database.
I see mytable_id_seq tables also, which now I know are called sequences in postgres terminology.
When I created a dump of those three tables, and restore them, do I have to do anything with the _id_seq sequences ?
Do I have to restore them too, for the dump data to work as it did in the original table?
When you restore the entire database from a dump, it contains CREATE SEQUENCE statements by default. These statements initialize sequences to the proper state. But if you make a partial dump, with only selected tables, you must set sequences manualy.
Assuming, that your table's name is "clip", you can check the current value using this query:
SELECT last_value FROM clip_id_seq
And if you want to update the sequence after restore, you can do it with this simple query:
SELECT SETVAL('clip_id_seq', SELECT MAX(id) FROM clip)
pg_dump -d database_name -t mg_cnd -F c > database_backup.sql
pg_restore -U database_user --data-only -d database_name -t mg_cnd -F c <file_location>
-d = database name
-t = table name
-F = Format
c = plain
-U = User
--data-only = transfer only table data
pg_dump documentation
pg_restore documentation

PostgreSQL: How do I backup database with name A and load it to database with name B?

I have two databases on the same server. One named A and one named B. Booth databases have the same structure. I want to empty database B and load it with data from database A. Which is the best way to do this?
I have tried to take backup of database A in plain format. Then open the resulting sql-file and replace every occurence of 'A' with 'B' and then run the sql-script. This worked but I think it should be an easier way to move data from one database to another. Is it?
I use 'pgAdmin III' as my tool, but this is not necessary.
This is my first post here, hope the question is relevant and structured well enough. I tried google first but found it hard to find anyone with the same question.
Thanks in advance!
/David
SOLUTION: After help from Craig, this is how I did it
pg_dump -Fc -a -f a.dbbackup A
psql -c 'TRUNCATE table1, table2, ..., tableX CASCADE'
pg_restore dblive.backup -d B -c (not sure if -c was necessary)
Backup:
pg_dump -Fc -f a.dbbackup
Restore:
psql -c 'CREATE DATABASE b;'
pg_restore --dbname b a.dbbackup
Use the -U, -h etc options as required to connect to the correct host as the correct user with permissions to dump, create and restore the DB. See the docs for psql, pg_dump and pg_restore for more info (they all take the same options for connection control).