'Not valid archive' when using pg_restore - postgresql

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.

Related

Importing SQL file: PostgreSQL

I'm a web development student and struggling to solve this problem on my own, any help would be appreciated!
Note that I'm working in an Amazon Cloud9 instance. My problem is that when I try to import an SQL file into a PostgreSQL database using the following command:
$ psql -d my_database < file_to_import.sql
I get the following error:
https:/raw.githubusercontent.com/...[removed for privacy]: No such file or directory
I know the file exists, because I'm able to navigate to it. I've tried copying the contents of the file into a new file on my desktop and then inserting the path to that file in the place of "file_to_import.sql" but that's not working either. I get the same error.
I've also tried importing via this template:
my_database=# \i ~/some/files/file_to_import.sql
But I get the same error. What's gone wrong here?
Thanks in advance!
These issues can be occurred because lack of permissions for a file try one of the following commands with the proper elevated permissions. For number one, you don't need sudo but if that didn't work try the second one of them should help you
1. psql -h hostname -d databasename -U username -f file.sql
2. sudo -u postgres psql db_name < 'file_path'

How to restore database from .pg_dump file using pgAdmin

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

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.

Pg_restore gives no errors in terminal, but doesn't seem to change anything

I'm having problems using pg_restore to while creating new DB for testing purposes.
What I did:
I've taken dump file created with pg_dump oldDB -f /some/directory/oldDUMP.dmp from my daily backup
I've logged to postgres user and created new DB with createdb -T template0 newDB
then I've tried to use (while being in the folder with my oldDUMP.dmp file) pg_restore -d newDB oldDUMP.dmp but output told me, that since dump is in txt, then I should log in to psql
I logged in as postgres (psql) and typed pg_restore -d newDB oldDUMP.dmp again. Nothing showed up (seemed like it worked) but there is no differences - test database is still empty.
I've tried to do the same from other user, and tried to log to psql to newDB (since as I understand sole psql command logged to postgres because I was logged as postgres on Linux) and still there were no differences. I'm not sure where to look for, for some logs with possible errors.
I've been worried I somehow overwrited some old DB, but I doubt that's possible.
Any idea what I'm doing wrong?
I'm new to Postgresql/psql (and Linux), so it's probably some very basic problem, but I wasn't able to find such problem in Web.
but output told me, that since dump is in txt, then I should log in to psql
You need to give psql the script as an input. Something like this:
psql < oldDUMP.dmp
I logged in as postgres (psql) and typed pg_restore -d newDB oldDUMP.dmp again. Nothing showed up (seemed like it worked) but there is no differences
Did you type pg_restore in the psql prompt? That's the wrong place. pg_restore is a program, not a SQL statement. The reason why nothing happened is probably because you forgot to terminate the statement with a ; - if you do that you will get a SQL syntax error.

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.