How To Restore Specific Schema From Dump file in PostgreSQL? - 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.

Related

Postgres: using flag --no-security-labels for loading data into db

I have sql-extension db-file and I need to load it using the --no-security-labels flag.
I usually load data from file with the command:
psql -U postgres -d db_test < db_test_dump.sql
If I try
psql -U postgres -d db_test --no-security-labels < db_test_dump.sql
then I get an error:
psql: unrecognized option `--no-security-labels'
I also tried like this:
pg_restore -d db_test -U postgres --no-security-labels db_test_dump.sql
but that doesn't work either:
pg_restore: error: input file appears to be a text format dump. Please use psql
How to upload data to db from .sql file with flag --no-security-labels?
What is "sql-extension db-file"? And how does that relate to the rest of your question?
That option is only available on pg_dump and pg_restore. You can redo the dump with that flag, or redo the dump in custom format (or one of the other formats supported by pg_restore) and then supply that flag to pg_restore. But either way, you need to redo the dump. If you don't have access to the original database, perhaps you can set up a scratch database, load the dump to it, and dump from there with the correct option or in the correct format.
Or you could manually edit the existing dump file to remove the security label bits.

Clean restore from PostgreSQL dump

I want to restore a database from backup and rewrite all data that is there with backup data.
My current command is like this:
pg_restore -h localhost -U postgres -d dbName -v autobackup_file.dmp
How to restore and rewrite all data?
I've seen an option -c; is that the correct way?
And where should I put it in my command?
-c can be anywhere, e.g. immediately after pg_restore.
It will DROP all restored objects before restoring them, but it will not drop any objects that are not in the dump.
To drop and recreate the whole database so you get a clean copy, you can use -C -c.

How to restore pg_dump file into postgres database

So I regularly backup and restore databases and schema's using pgadmin4. I would like to do it with a batch file using commands as pg_dump and pg_restore. I however always fail to succeed in this and could use some help. The way I try to dump one schema (with data) is the following:
pg_dump -U postgres -F c -d database -n schema > mw2
Then I try to restore it with pg_restore:
pg_restore -U postgres -d otherdatabase -n schema mw2
First I tried to use .dump in stead of tar but the result stays the same; which is an empty schema in my database.
Or the following error message:
pg_restore: [archiver] input file does not appear to be a valid archive
https://www.postgresql.org/docs/current/static/app-pgrestore.html
--format=format Specify format of the archive. It is not necessary to specify the format, since pg_restore will determine the format
automatically. If specified, it can be one of the following:
custom, directory and tar
https://www.postgresql.org/docs/current/static/app-pgdump.html
--format=format
Selects the format of the output. format can be one of the following:
p plain Output a plain-text SQL script file (the default).
others are custom, directory and tar
in short - you used defualt plain format, which is meant for using with psql, not pg_restore. So either specify different format with pg_dump or use your file as
psql -f mw2.tar

Is it possible to restore PostgreSQL data without overwriting existing data?

I have 2 backup files and I want to merge all this data on my database.
I try to use pg_restore but when I use with the second database file I lost the first data set.
Look for my command:
pg_restore -U postgres -c --if-exists -d ravpacheco_db "C:\Users\ravpacheco\xpto1.backup"
I also search all options flags for pg_restore command but I can't find some usefull thing
My problem is with my pg_dump command. If I created a backup file only with data my restore will work
Now I'm using this pg_dump command
pg_dump --column-inserts --data-only --table=<table> <database>
I resolved my problem using this stackoverflow thread

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