How to restore a PostgreSQL database from within an SQL script - sql

How to restore a PostgreSQL database from within an SQL script.
I want to be able to restore a PostgreSQL using SQL or psql code.

Assuming you are working with pg_dump, then it can depend on how it was dumped.
If it was dumped to a text or sql file:
psql -U [user] -d [dbname] -1 -f <filename>.sql
Alternatively you can use pg_restore:
pg_restore -U [user] -d [dbname] [filename].dump
The following postgres docs might be helpful as well.

Related

Import dump file containing a database dump to dbeaver

I have a database dump in thisdb_2022.dump binary file that I'm trying to import to dbeaver, but I haven't found a way to import the database so I can see it.
I found the below in the dbeaver forum but when I try to follow the instructions and create a new connection I don't see any option I can select that will open this document.
https://dbeaver.io/forum/viewtopic.php?f=2&t=895
Edit: The database and version is PostgreSQL 12
. I'm not trying to
dump it to an existing db rather I want to create a new one with this
dump.
the dump command looks like this: pg_dump -h blah.amazonaws.com -Fc -v --dbname="blah2" -f "/tmp/dump/20220203.dump".
And it will be the same version PostgreSQL 12
The easiest way to not use DBeaver at all.
Do:
UPDATED with correct command.
--In psql
CREATE DATABASE new_db;
--Exit psql
--At command line
pg_restore -d new_db -h <the_host> -p <the_port> -U postgres /tmp/dump/20220203.dump
To work in Dbeaver directly see Backup/Restore.

Extract data from postgres using bulk loading

CMD.EXE /C BCP "SELECT * FROM ci.trn_td_cash" queryout D:\Sample\data\Load\CASH.COPY -c -t"|" -r"\n" -S xxx.xxx.xxx.xx -d finance -U user -P 1111 .
I want to extract data from postgresql using bulk loading BCP command.Can i use BCP for posgresql. I use the following command for bulk loading. But it is not connecting to database.
please help me with syntax.
COPY Command in PostgreSQL:
The COPY command allows high-speed bulk data transfer to or from the server. Copy-in and copy-out operations each switch the connection into a distinct sub-protocol, which lasts until the operation is completed.
COPY command is supported in PostgreSQL Protocol v3.0 (Postgresql 7.4 or newer).
For more info follow the below link:
https://www.postgresql.org/docs/current/static/sql-copy.html
https://www.postgresql.org/docs/current/static/protocol-flow.html#PROTOCOL-COPY
GENERAL SYNTAX:
COPY (<select-query-here>) TO <file-path>;
Here is one answer I found to your question of how to run it:
https://ieftimov.com/postgresql-copy
Try this One:
\copy (note the backslash) lets you copy to/from remote databases and does not require superuser privileges.
psql -h remotehost -d remote_mydb -U
myuser -c "\copy mytable (column1, column2) from '/path/to/local/file.csv' with delimiter as ','"

Create and import pgsql database after pg_dump

I am new to postgres. I have exported a large, complex database with the following command in the terminal
pg_dump -U USERNAME DBNAME > dbexport.pgsql
Now that I have transferred this .pgsql file to a different computer, what is the right command to automatically create and restore the exact same database as was exported? Any suggestions would be appreciated
The way you dumped the database, the information about the database itself is not included in the dump (which is a plain SQL file).
You can either use the -C option to include CREATE DATABASE in the dump (the dump has to be restored with psql), or you use the custom format:
pg_dump -F c -U postgres DBNAME -f dbexport.pgsql
That can be restored with pg_restore like this:
pg_restore -C -d postgres -U postgres dbexport.pgsql

Can not import database in postgresql

Hi i am using postgresql . i tried to import a databsee by
$ psql arbles < app.arbles.com.15.08.2014.sql;
ERROR: syntax error at or near ""
LINE 1:
^
it gives me an error ,
I firstly created my database arbles
then i gave one of my database user's full privilages of the database
grant all privileges on database arbles to postgrestest;
3.then i tried the first command and it failed
my app.arbles.com.15.08.2014.sql file is in /var/lib/postgres i also moved it to different locations , but the same error occures.
postgrestest
is also a super user
i also thired with different solutions
1. psql -h hostname -d databasename -U username -f file.sql
2. \i C:/database/db-backup.sql
non of them worked , why is this happening , please help me , thanks in advance
As you indicated that the backup was created using pg_dump, then, most likely, it's not a SQL file. Instead of trying to execute SQL from that file, use pg_restore to restore it:
pg_restore -h hostname -d databasename -U username filename

pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist

ok, following these instructions I've runned:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
in the same folder where the database is (app_db.sql) but I keep getting:
pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Why is this happening and what can I do solve it?
Thanks
You have critically misunderstood how PostgreSQL works.
app_db.sql is not a database, it is a database dump. A sequence of text commands that describe the data in the database that can be replayed to create the database.
It isn't like a Microsoft Access .dbx file or a SQLite3 database file, a database stored in a single file that can be manipulated directly. The only way to work with a PostgreSQL database is via a client/server connection to the PostgreSQL server, which stores the actual database contents in a system dependent location like /var/lib/pgsql which you never need to manipulate directly.
To use the database dump, you must restore it into a new empty database using the psql command, eg:
$ createdb mydb
$ psql -f app_db.sql mydb
This will probably fail with permissions errors if you try to run it exactly as written above. You will need to create yourself a user, give that user ownership of mydb, and possibly edit pg_hba.conf to allow yourself to authenticate depending on your system settings.
A more realistic example for a user with unix login name bob might be:
$ sudo -u postgres createuser bob
$ sudo -u postgres createdb -O bob mydb
$ psql -f dpp_db.sql -1 -v ON_ERROR_STOP=1 mydb
I strongly recommend that you read the PostgreSQL tutorial and the user manual.