How to restore database from .pg_dump file using pgAdmin - postgresql

I was given a mydb.pg_dump file. I have created a new database test1 with pgAdmin. When I click right on the database and select Restore..., I can select my file after selecting "All files". But when I attempt to restore the database I immediately obtain this error:
pg_restore: error: input file appears to be a text format dump. Please
use psql.
Can I use pgAdmin to restore a .pg_dump file? How?

You cannot use pgAdmin for that, because in a plain-text dump COPY statements are mixed with COPY data.
You need psql for that, which fortunately is already installed on your machine:
psql -d mydb -U postgres -f mydb.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.

'Not valid archive' when using pg_restore

As someone who is very new to working with databases/SQL I'm having trouble setting up a dump from the database Qscored, which can be found at https://zenodo.org/record/4468361#.YgTTZ-7ML0p, the first dump (ab) is the one I've worked with. The downloaded file is of type POSIX tar archive. According to the README file, these are the commands to be run in a terminal window.
cat qscored_dump_25Jan2021ab > qscored_dump_25Jan2021.tar
psql -U postgres
When in a PostGreSQL user run
CREATE DATABASE qscoreddb WITH TEMPLATE=template0 ENCODING='UTF-8';
CREATE USER dbwala WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE qscoreddb TO dbwala;
Then, in a new terminal window I run pg_restore according to
pg_restore -d qscoreddb -U postgres \path\to\qscoreddb_dump_Jan252021.tar
I then get the error message
pg_restore: error: input file does not appear to be a valid archive
Does anyone know how I might be able to solve this problem or does it mean there are problems in the original file?
I'm using PostGreSQL 14 with a Mac Monterey 12.2.

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.

Backing up PostgreSQL Database in Linux

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.