Partition table data are not restored using pg_restore in PostgreSQL - postgresql

Need Help !!
While i'm going to restore a partition table using :
pg_restore -U (username) -d (Database) -p (port_number) -t (partition_table) -f (filename)
the result is success but the data in that partition table is not restored, can anyone help me ?

When -t is specified, pg_restore makes no attempt to dump any other database objects that the selected table(s) might depend upon. Therefore, there is no guarantee that the results of a specific-partitioned table dump can be successfully restored
However you can take dump using
pg_dump --host=hostname --username=user -t "child_table_*" --data-only --file=dump_out.sql dbname
and then restore it, This is probably a consequence of the design decision to have partitions visible as individual tables on the SQL level.
please refer:
https://www.postgresql.org/docs/13/app-pgrestore.html
https://www.postgresql.org/docs/11/app-pgdump.html

Related

If I drop my heroku table, can I later restore the database with a Heroku backup?

I'm having some issue with my migrations in Heroku. I've tried several things but none seem to work.
I feel like I should just drop the table that's causing the issue, which will delete all the data in production.
If I drop the table, and re-created the table, will I be able to restore all of the data I lost? Because I will backup my database on Heroku before I drop the table.
Thanks!
You should run a backup with
pg_dump -h hostname -p 5432 -U username -F c -t mytable -f dumpfile mydatabase
Then, after you have dropped and re-created the table, you can restore the data with
pg_restore -h hostname -p 5432 -U username -a -d mydatabase dumpfile
However, this will not work if the table structure has changed.
In that case, you might want to use COPY directly to write the data to a file and restore them from there.
Let's for example assume you plan to add another column. Then you could dump with
COPY (SELECT *, NULL FROM mytable) TO '/file/on/dbserver';
After the table was created with the new column, you can
COPY mytable FROM '/file/on/dbserver';
The new column will be filled with the NULL values.
Modify this basic recipe for more fancy requirements.

can I force usage of specific tablespace for singe pg_restore task?

is there some clever way to force usage of specific tablespace for pg_restore task in situation when I need to run independently in parallel several pg_restore tasks and I would need to direct some of them into tablespace on SSD and others into specific tablespaces on other standard disks?
We got this use case - daily during the night we need to copy daily partitions into new warehouse database. Standard pg_dump / pg_restore is currently used (logical replication is currently not possible for internal policy reasons although it would be highly desirable).
More pg_restore tasks run in parallel on target database and I would need to set specific target tablespace for specific task - therefore global "default_tablespace" setting does not help. I also cannot reconfigure source database to have proper tablespace directly in dump not to mention that with growth of warehouse DB I would need to change target tablespaces from time to time.
Originally I thought PG env var "PGTARGETSESSIONATTRS" could maybe help me to set "default_tablespace" for specific session of pg_restore but looks like this var cannot do it.
Databases are PG 10 (source) and PG 11 (target).
Based on comment from #a_horse_with_no_name I tested this sequence of commands which does what I need:
pg_dump -U user1 -v -F c --no-tablespaces -t schema.table database1 -f exportfile
pg_restore -U user2 -v --no-tablespace -d database2 --schema-only -c exportfile
psql -U user2 -d database2 -c "alter table schema.table set tablespace xxxx"
pg_restore -U user2 -v --no-tablespace -d database2 --data-only exportfile

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.

PostgreSQL - checking if restored database has all the data as original

I backup my PostgreSQL database once a week. One time, during the restore, the restored database was 2.9GB in size, while original database size was 3.7GB. The reason for different sizes is well explained here and that's not a problem.
I back up database using:
pg_dump -U postgres database_name -f backup_file.sql.
To restore the database I use:
psql -U postgres -d target_database -f backup_file.sql.
My question is what is the best way to test and ensure that the restored database has all the data from original database?

postgres copy tables from another database using pg_dump?

I would like to copy two tables from database A to database B, in postgres
how can I do it using pg_dump without losing the previous tables and data in database B ?
I read some answers in Stack Overflow suggesting using pg_dump but in the documentation page I read?
The idea behind this dump method is to generate a text file with SQL
commands that, when fed back to the server, will recreate the database
in the same state as it was at the time of the dump
Doesn't that mean it will delete the previous data in database B?
If someone could tell me step by step solution to move two tables in database A to database B without losing any previous data in Database B, it would be helpful.
I found the answer to my question :
sudo -u OWNER_USER pg_dump -t users databasename1 | sudo -u OWNER_USER psql databasename2
if you pg_restore a database into b database, of course a will replace b. instead pick specific table you would like to restore using pg_restore -t
you could pg_restore to different schema, by using -O (no_owner)
so let say
pg_dump -Fc -f dump.dmp -v -h host -U user_login -n schema_to_dump
you can
pg_restore -v -h host -U user_login -n schema_to_import -a --disable-triggers dump.dmp