Backing up PostgreSQL Database in Linux - postgresql

I have to create a postgresql database back up(only the schema) in Linux and restore it on a window machine. I backed up the database with option -E, but I was not able to restore it on the window machine.
Here is the command that I used to backup the database
pg_dump -s -E SQL_ASCII books > books.backup
below is the error message that I received when I tried to restore it.
C:/Program Files/PostgreSQL/9.3/bin\pg_restore.exe --host localhost --port 5432 --username "postgres" --dbname "books" --role "sa" --no-password --list "C:\progress_db\test1"
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
Am I supposed to use a different command or am I missing something? Your help is greatly appreciated.

The first lines in the official docs about restoring say:
The 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
where infile is the file output by the pg_dump command
Or simply read the error message.
The option -E SQL_ASCII only sets the character encoding, it has nothing to do with the format of the dump. By default, pg_dump generates a text file that contains the SQL statements to regenerate the database; in this case, to restore the database you only need to execute the sql commands in the file, as in a psql terminal - that's why a simple psql dbname < mydump is the way to go.
pg_restore is to be used with the alternative "binary" -postgresql specific- format of pg_dump.

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.

Postgresql - backup restore on different owner?

My question is similar to this I even vote up it, but unfortunately in my case I have text dump and I can not restore using pg_restore =(
zcat /home/kes/work/projects/bennet/db/bennet.sql.gz | \
pg_restore -h 127.0.0.1 -p 5432 -U bennet --no-owner --role=bennet -d bennet
pg_restore: error: input file appears to be a text format dump. Please use psql.
But psql has no role and no-owner options
Is there a way to restore text dump on different owner?
No. You have two options:
Manually edit the SQL script. This is cumbersom and error-prone.
Restore the complete dump to a new, empty scratch database using psql and dump that with --role and --no-owner as you need.

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

PostgreSQL: import/restore database dump (pg_dump version 9.6.5) on Windows

I am a newbie and I have a postgresql large dump of type .sql and i want to import it. I am on Windows 10 and haven't been able to find solution.
I tried to restore using pgAdmin3 but it doesn't show .sql file while restoring. I also found few commands and tried them but nothing seems to work.
I also tried loading the datasource in IntelliJ DataGrid but it doesn't show the correct driver during the loading settings.
Can someone help?
First, figure out if the dump was created with pg_dumpall or pg_dump.
Dumps from pg_dumpall start with:
--
-- PostgreSQL database cluster dump
--
If the dump was created with pg_dump, find out if the -C option was used.
If yes, the dump will contain a line with a CREATE DATABASE statement.
To restore, use psql from the DOS box. I assume that psql is on your PATH.
A dump from pg_dumpall or pg_dump -C is restored with
psql -U postgres -d postgres -f dumpfile.sql
A dump from pg_dump without -C is restored with
psql -U postgres -d mydatabase -f dumpfile.sql
where mydatabase should be replaced with the name of the target database into which the dump should be restored.

Postgres pg_restore command to restore complete schema

am using Postgres EnterpriseDB 9.5.0.5
I have taken a schema dump by using the below command
pg_dump -n 'schema1' db1 > schema1.dump
Now i want to restore it in different database (db2) what is the command i have to use.
i tried
pg_restore -d DB2 schema1.dump;
but it is showing error
pg_restore: [archiver] input file does not appear to be a valid archiver
You have two choices:
if you include no -f option for pg_dump, it creates a sql script so then restore using psql, not pg_restore
You could add -fc to create a custom binary/compressed format and then use pg_restore as you are trying to do.
However pg_restore mostly converts the archive to an SQL script, so it is not useful when you start with an sql script.
pg_dump -Fc mydb > db.dump
pg_restore -c -d mydb db.dump