Restore database from pg_dump syntax errors - postgresql

all I am trying to restore my db from pg_dump that I got from my server. I know there are plenty similar question, I tried what was suggested but still can not resolve my issue.
I created my pg_dump via this command:
pg_dump name_of_database > name_of_backup_file
Then I create new db:
createdb -T template0 restored_database
Then I restore using this command:
psql -1 -f name_of_backup_file.sql restored_database
It runs and then I got different syntax errors, for example:
psql:nurate_pg_dump.sql:505: invalid command \nWatch
Query buffer reset (cleared).
psql:nurate_pg_dump.sql:512: invalid command \nThe
psql:nurate_pg_dump.sql:513: invalid command \N
psql:nurate_pg_dump.sql:4098: ERROR: syntax error at or near "9"
LINE 1: 9 the course is not difficult, but Zauresh Atakhanova is not...
I believe since I made pg_dump from the same server, and try to restore it there nothing changed in my server setup, or version of postgres, so I think my db should restore correctly. How can I resolve these syntax errors?
EDIT: Ok, the problem is not with the syntax:
CREATE EXTENSION
ERROR: must be owner of extension plpgsql
This error is thrown at this line:
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

try to restore your database with
psql restored_database < name_of_backup_file

Related

'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 avoid 'invalid command' and 'out of memory' errors when loading .sql file into postgres database?

I'm trying to dump a .sql file into a PostgresSQL database and it's acting kinda funky. I've followed a lot of the advice here (Import SQL dump into PostgreSQL database) and thought I got close. Whenever I try something like:
psql -h localhost -d db_name -U postgres -f sql_file.sql
It starts to work, but then I start to get a bunch of errors in a row like
psql:sql_file.sql:42: error: invalid command \'s
psql:sql_file.sql:43: error: invalid command \'t
psql:sql_file.sql:44: error: invalid command \'s
With that final invalid command changing to different strings on the end there. Is it not understanding some escape characters and instead trying to use them as commands in psql? If so, how do I avoid this? Eventually the dump terminated with:
psql:sql_file.sql:2281: error: out of memory
It is a rather large sql file (about 150GB). Am I going about this the wrong way? Any advice?

Postgres import database from file syntax error

I'm trying to import postgres database from dump file using command:
psql -d plus_se -f se.sql -v
I'm getting error:
psql:se.sql:22: ERROR: syntax error at or near ""
LINE 1: DROP TABLE IF EXISTS `DATABASECHANGELOG;
The dump was made using pg_dump and last time (on previous Ubuntu, I've recently installed fresh env) I was able to recreate database using this file.
Can anyone have a clue what might be wrong?

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.

I always get an error when typing commands in psql but not if I copy past the command

Every time I type a command in psql it throws an error, but if I copy and paste the exact same command it works. For example, I typed this:
# GRANT ALL PRIVILEGES ON DATABASE db_name TO user;
and got:
ERROR: syntax error at or near "GRANT"
LINE 2: GRANT ALL PRIVILEGES ON DATABASE db_name TO user;
^
Then I did a copy/paste on a new line with:
# GRANT ALL PRIVILEGES ON DATABASE db_name TO user;
and it worked:
GRANT
The above is copied exactly from terminal changing only the db_name and user.
This happens every time I try to type in a command. Using 10.8.2 and PostgresApp from Heroku.
Any ideas?
mu is too short answered this in the comments. I was indeed just forgetting to add the semicolon then retyping the command. It seemed that this was happening every time because I had only been using psql for about an hour.
In my case, the same error occurred but with having a semicolon at the end, I had to quote the params like:
sudo -u root psql -c "GRANT ALL PRIVILEGES ON DATABASE \"my_database\" TO \"my_database_user\";"
I also could verify this via pgAdmin/Database Properties/Security Tab/