Create and import pgsql database after pg_dump - sql

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

Related

How to execute multiple SQL files from a local directory in PostgreSQL/PgAdmin4 in on transaction in Windows?

I have a folder in a directory in my PC which contains multiple SQL files. Each of the file is a Postgres function. I want to execute every SQL file situated in the folder at a time in PostgreSQL server using PgAdmin or in other way. How can I accomplish this?
I apologize if I'm oversimplifying your question, but if the main issue is how to execute all SQL files without having to call them one by one, you just need to put them in a loop, e.g. in bash calling psql
#!/bin/bash
for f in *.sql
do
psql -h dbhost -d db -U dbuser -f $f
done
Or cat them and pipe the result to psql stdin:
$ cat /path/to/files/*.sql | psql -h dbhost -d db -U dbuser
And if you need them to run in a single transaction, consider merging the SQL files, e.g. using cat - this assumes all statements in your sql file are properly terminated:
$ cat /path/to/files/*.sql > merged.sql

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 ','"

How to restore a PostgreSQL database from within an SQL script

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.

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.