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

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

Related

Partition table data are not restored using pg_restore in 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

How to pg_restore one table and its schema from a Postgres dump?

I am having some difficulties with restoring the schema of a table. I dumped my Heroku Postgres db and I used pg_restore to restore one table from it into my local db (it has more than 20 tables). It was successfully restored, but I was having issues when I tried to insert new data into the table.
When I opened up my database using psql, I found out that the restored table is available with all the data, but its schema has zero rows. Is there anyway I could import both the table and its schema from the dump? Thank you very much.
This is how I restored the table into my local db:
pg_restore -U postgres --dbname my_db --table=message latest.dump
Edit:
I tried something like this following the official docs, but it just gets blocked and nothing happened. My db is small, no more than a couple of megabytes and the table's schema I am trying to restore has no more than 100 row.
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
As a more general answer (I needed to restore a single table from a huge backup), you may want to take a look at this post: https://thequantitative.medium.com/restoring-individual-tables-from-postgresql-pg-dump-using-pg-restore-options-ef3ce2b41ab6
# run the schema-only restore as root
pg_restore -U postgres --schema-only -d new_db /directory/path/db-dump-name.dump
# Restore per table data using something like
pg_restore -U postgres --data-only -d target-db-name -t table_name /directory/path/dump-name.dump
From the Heroku DevCenter here
Heroku Postgres is integrated directly into the Heroku CLI and offers
many helpful commands that simplify common database tasks
You can check here if your environment is correctly configured.
In this way, you can use the Heroku CLI pg:pull command to pull remote data from a Heroku Postgres database to a local database on your machine.
For example:
$ heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi

Move data from a postgres database to another database with a different owner

I have two postgres databases with the same structure and tables, hosted on the same server, the databases are owned by different users:
database1: owner1
database2: owner2
I want to know the best way to copy the content of database1 into database2 (overriding the original content of database2).
I tried pg_dump & pg_restore but the dump will explicitly specify owner1 as the owner of the tables and then I have permission issues when trying to get the data from database2 using owner2. I had to manually re-grant all privileges on database2 to owner2 and set the owner of all tables again to owner2.
My approach:
pg_dump database1 > database1.psql
postgres=# drop database database2;
postgres=# create database database2;
psql --d database2 -f database1.psql
Is there a simpler way to copy the data from database1 to database2 without having to update the user permissions manually after the restore.
Yes, you can use pg_dump to specify that you don't want to export ownership:
-O (or --no-owner) does not export ownership
-x (or --no-privileges) Prevent dumping of access privileges (grant/revoke commands)
pg_dump db_name -O -x > output_file

Copy only constraint Keys from one database to another in pgAdmin

I have two servers (and two databases each one from a server on pgAdmin):
The first server is for testing and the second one is the real server that the client will work on it.
The two databases have same tables: 9 tables on the first one and the same 9 tables on the second one.
But on the test server, I have constraint keys (Check, foreign, unique, not null) that I have tested and ready to be deployed on the server 2.
How i can copy only those constraints?
Thank you so much.
The simplest way to do this is to use a command-line pg_dump tool, which is included in all PostgreSQL installations.
Create a backup of the target database:
$ pg_dump -h /var/run/postgresql/ -d prod -Fd -f prod.pg_dump
Create a backup of the source database schema:
$ pg_dump -h /var/run/postgresql/ -d test --section=pre-data --section=post_data -Fd -f test_schema.pg_dump
Drop the target database:
postgres=# drop database prod;
Recreate empty target database:
postgres=# create database prod;
Import to the target database:
test table definitions:
$ pg_restore -d prod --section=pre-data test_schema.pg_dump
prod data:
$ pg_restore -d prod --section=data prod.pg_dump
test constraints and indexes:
$ pg_restore -d prod --section=post-data test_schema.pg_dump
Analyze the target database:
prod=> analyze;
This can be optimized with parallel restore with proper usage of pg_restore -j.
You might consider a proper schema change management solution for the future.

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