How do I restore a .sql file backup in pgadmin - postgresql

I have a .sql file of the database backup. PostgreSQL is installed in my system and I use pgadmin4 to communicate with database. I want to restore this .sql file using pgadmin. Can someone please guide me.

An SQL script that was generated with pg_dump without the --inserts option cannot be restored with pgAdmin, because the COPY data are mixed with the statements. You need psql for that.
You could use one of the other backup formats ("custom", "directory" or "tar") and use pgAdmin to restore that. But all that pgAdmin does in this case is call pg_restore, so you might as well do that yourself from the command line.
Another option is to launch the "psql tool" from pgAdmin, which is nothing else than psql. Then you can load the dump with \i dumpfile.sql.

Related

Problem restoring databse between 2 RDS instances using pg_dump and pg_restore

I'm having difficulty restoring a DB to an AWS RDS Postgresql instance. Context is that i am backing up from one RDS instance and restoring to another RDS insurance. They both have the same version of Postgresql 9.6.5.
I was able to take a dump using the following command:
./pg_dump.exe -U dbuser -W -h prod-pgsql-rds.3ft5coqxjdnq.eu-west-2.rds.amazonaws.com -d devdb > c:\tmp\backup.sql
From the resulting .sql file, I then attempted a restore to another RDS instance which is also using Postgresql 9.6.5 using below command:
./pg_restore.exe -U dbuser -d testdevdb -h dev-pgsql-rds.cym8coqx52lq.eu-west-2.rds.amazonaws.com "c:\tmp\backup.sql"
*I also tried the -f switch in the above restore command instead of the " " quotes before/after the file name
But when I try to restore it to a newly created database I get the following error:
pg_restore: [archiver] input file does not appear to be a valid archive
Can anyone help? FYI, I am using PGAdmin 4 via Windows PowerShell. I have to edit some of the values in the strings above due to data sensitivity.
pg_restore is only used for the other, non-plain-text output formats that pg_dump can output. For .sql dumps, you just use psql. See the docs on restoring from backups.
In a Unix env, you'd do psql [yourflags] < /tmp/backup.sql, but I'm unfamiliar with powershell and don't know if it supports < for input redirection; hopefully either it's present or you know the equivalent PowerShell syntax.
So I couldn't get psql or pg_restore to work so opted to import the .SQL file into via the SQL query tool in PGAmdin. This through up some errors so had to make several changes to the .SQL file and perform below:
Commented out a couple of lines that were causing errors
Elevated permissions for the user and made him the owner of for the Schema and DB properties by right-clicking on these via PGAdmin
The .sql file was making several references to the user from the source RDS DB so had to do a find and replace with a user account created for the destination RDS DB. Alternatively, I could have just created a new user on the destination DB with the same username and password as the source DB and then make him the owner in ref to step 2.

How to view pg_dump data with .sql extension?

I have downloaded pg_dump data from DeepDive Open Datasets. It is a file with extension .sql and to my knowledge that is a text file with SQL commands in it to recreate the database. I'm trying to peruse the data. How do I set up a Postgres database to do that?
I tried to use pgAdmin4 to view the data. However, I'm not sure how to set up and add a new server to read the data. I'm not sure if that is the correct approach.
I would appreciate any guidance with this.
You would view the dump itself with a pager like more or less; such a dump is a plain text file with SQL statements.
To restore such a dump you need to use psql, the command line client:
psql -U postgres -d adatabase -f dumpfile.sql
pgAdmin cannot restore such a dump, because it contains COPY ... FROM STDIN statements intermixed with the data.

how to restore a single "View" in postgresql

I know how to restore a single table form postgresql database backup file.
For Example: To restore password_history table i use the below command and it works fine.
pg_restore --data-only --table=password_history C:\Users\sjayaram\Desktop\dcmdb-01-17-2014-10-19-18.backup > passwordhistory.txt
Whats the command to restore a single "View" from postgresql database backup file.

Using psql command line terminal program to update database

Can someone give the steps to updating my database using psql command line terminal program?
I have created a PostgreSQL database in pgAdmin and I have backed it as PLAIN file (plain-text script). I can't restore that file in pgAdmin. In this this website, it says you can execute a plain-text script file using the "psql command line terminal program", to recreate the database and load data.
So I'm just wondering if someone can give steps to doing this so I can update my current database (outside of pgAdmin).
Run in a shell of your database server as user postgres (or any other user with the necessary privileges):
postgres#db:~$psql
CREATE DATABASE mydb;
\c mydb
\i /path/to/backup.sql
Thereby you create a database, connect to it and run the plain text SQL script from the file to restore the contents.
Details about psql in the manual.

pgAdmin III Restore PLAIN Backup

New to PostgreSQL database, I found column names were automatically converted to lowercase. I created a PLAIN backup, manually edited the column names to proper case. When I tried to restore the edited PLAIN backup, the OK button of pgAdmin3 Restore was disabled.
Is there a way run the edited plain backup file? Please help.
Use the client program psql to restore, works fine. The installers of pgAdmin and PostgreSQL also install psql, so you should have it on your pc or your database server.
psql -f backup.sql postgres