pg_dump ignoring schema specified with -n - postgresql

I am using pg_dump (PostgreSQL) 9.2.4.
I have a database with two different schemas that have identical table names.
- mydatabase
- schema_a
- mytable
- someothertable
- schema_b
- mytable
- another table
I want to copy both schema_a.mytable and schema_b.mytable from orig_host to new_host. I log into new_host and type:
% psql -c "drop schema schema_a cascade" mydatabase
% psql -c "create schema schema_a" musicbrainz_db
% pg_dump -h orig_host -n schema_a -t mytable mydatabase | psql mydatabase
No problem, but when I do the same for schema_b, I get conflicts:
% psql -c "drop schema schema_b cascade" mydatabase
% psql -c "create schema schema_b" musicbrainz_db
% pg_dump -h orig_host -n schema_b -t mytable mydatabase | psql mydatabase
ERROR: relation "artist" already exists
I confirm by dumping this last command to a file that it is setting the search path to schema_a, which causes the failure. It does seem to work if I do
% pg_dump -h orig_host -t schema_b.mytable mydatabase | psql mydatabase
But shouldn't the -n switch work here?

From the manual -
The -n and -N switches have no effect when -t is used, because tables selected by -t will be dumped regardless of those switches, and non-table objects will not be dumped.
You might have schema_a in your search_path, which is why the first command works.

Take a look at the SQL produced. I believe what you should see is:
Everything in schema_a
All tables named *.mytable
That is, the options are additive.
I'm guessing the intent is that schema S might well depend on one or two other tables so you can dump them all together.

Related

pg_dump Command Issue

I have been using pg_dump for a while and every time I try to run the same script I seem to get issues. Not sure if it is user error or something to do with updating to Postgres 11.
Here is my command
pg_dump --dbname=postgresql://username:password#localhost:5432/DatabaseName --data-only --column-inserts -t "\"HoldingValuesTemp\"" > holdingValues.sql
This throws the error
pg_dump: too many command-line arguments (first is "HoldingValuesTemp\")
I think the issue has to do with the table name, it is case sensitive and is HoldingValuesTemp.
I tried to break it down into another pg_dump
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t "\"HoldingValuesTemp\"" > holdingValues.sql
Which gives the same error
So I also tried
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t '"HoldingValuesTemp"' > holdingValues.sql
after entering the password I get pg_dump: no matching tables were found
Any help would be appreciated.
My solution was more of a workaround than a solution.
The issue had to do with the table name, not sure why it was not finding that table but I assume it had to do with case sensitivity.
Solution:
Rename the table with a prefix that I looked up with a wildcard.
Table was "HoldingValuesTemp" I updated it to "ts_HoldingValuesTemp"
then ran the following command
pg_dump -d DatabaseName -p 5432 -U username --data-only --column-inserts -t 'ts_*' > holdingValues.sql
making it backup all tables that begin with "ts_"
Try to qualify the table name with the schema:
-t '"MySchema"."HoldingValuesTemp"'
There is also the possibility that you have a space character or similar in the table name.

Restore multiple tables postgresql pg_dump

I have a database with several tables and I want to add the data and structure of a selection of them to a different database containing already different tables.
I have created the dump file in the following way:
"C:\Program Files\PostgreSQL\9.1\bin\pg_dump.exe" -U postgres -w DBName -t table1 -t Table2 -t Table3 > "E:\Dump.sql"
This works fine and I have E:\Dump.sql with the dump of the three tables in it.
I have tried to make a restore with the following code:
"C:\Program Files\PostgreSQL\9.6\bin\pg_dump.exe" -C -U User -t table1 -t Table2 -t Table4 destdb < Dump.sql
but I get the error
no matching tables were found
.
Where am I doing wrong?
https://www.postgresql.org/docs/current/static/backup-dump.html
pg_dump is meant for **taking* backups, next section of manual is for restoring a backup:
Text files created by pg_dump are intended to be read in by the psql
program. The general command form to restore a dump is
psql dbname < infile
Also you mention three tables, two of which are mixed case - in order for it to work, you have to double quote it I believe. But anyway - restoring for you file would be:
psql YOURDBNAME -f E:\Dump.sql

Postgres - copying a table from a dump file

I have a database dump that I generated with pg_dump, like so:
pg_dump -C remote_db -a --no-owner -t my_table > dump.sql
and I'm looking to copy a single table over from it into my local database, with data only (not schema) and with no ownership settings. I'm familiar with how to do it directly from another db using pg_dump, something like:
pg_dump -C remote_db -a --no-owner -t my_table | psql local_db
But I'm not sure how to replicate the same effect from a file.
I've tried something like:
pg_restore -d local_db -a --no-owner -t my_table dump.sql
But got an error:
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
I'm not sure how to use psql to achieve the same thing. Help would be appreciated.

Postgres taking backup of master data and schema for few of the tables

In my database, I have the master tables starting with m_* and other. What I want to take the back of tables with following scenario.
Backup schema + data for master tables i.e table names starting with m_*
Backuo schema structure for the rest of the tables.
I did read the following command somewhere
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_*'
But I have so many tables and I find it tedious to put each table name in it. Any tidy way to get around it?
Using Linux:
File foo.sh (adjust filtering conditions):
psql <connection and other parameters> -c "copy (select format('--exclude-table-data=%s.%s', schemaname, tablename) from pg_tables where schemaname in ('public', 'foo') and tablename<>'t') to stdout;"
Command (note about backticks):
pg_dump <connection and other parameters> `./foo.sh`
Note that it is very flexible approach.

How do I do a schema only backup and restore in PostgreSQL?

How do I take a schema level backup in PostgreSQL database and restore on the another database? Is there any single command available for this? For example, can I pg_dump and restore in single line?
pg_dump --schema=masters oldDB > masters1.sql
cat masters1.sql | psql newDB
or
in single command you can do by this
pg_dump oldDB --schema masters | psql -h localhost newDB;
Backup schema and restore it on system for postgresql as below:
Dump schema for database
pg_dump -s database_name > db.sql
Dump schema for specific table
pg_dump -s database_name -t table_name > db.sql
Restore backed up schema using below command
psql -d database_name -h localhost -U postgres < path/db.sql
-s or --schema-only to exclude data from dump
Documentation
What's wrong with the documentation?
Example from the manual:
To dump all schemas whose names start with east or west and end in
gsm, excluding any schemas whose names contain the word test:
$ pg_dump -n 'east*gsm' -n 'west*gsm' -N 'test' mydb > db.sql