PostgreSQL DB dump location - postgresql

I want to download dumped PostgreSQL databases from an Ubuntu 16.04 server.
sudo su - postgres
pg_dump my_db > backup_db
Search for the path yields the following:
ps auxw | grep postgres | grep -- -D
postgres 7311 0.0 0.0 293332 3384 ? S Mai04 0:39 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
Yet I cannot find the dumped files there. What is the location of the dumped files?

$HOME of user postgres
pg_dump just echoes to stdout, unless you specify -f
-f file --file=file
Send output to the specified file. This parameter can be omitted for
file based output formats, in which case the standard output is used.
It must be given for the directory output format however, where it
specifies the target directory instead of a file. In this case the
directory is created by pg_dump and must not exist before.
(formatting mine)
so in your case file backup_db will be in same directory where you were running pg_dump my_db > backup_db
next time try specifying full path to know exact location

Related

Error in run sql file in postgres | No such file or directory

Problem
when in tried run sql file in psql shell...
give "No such file or directory" error!
$ ls
config.sql config.yaml
$ sudo -i -u postgres psql
postgres=# \i config.sql
config.sql: No such file or directory
thanks for your reply!
Quick solution:
-i => goes to user's home directory!
as result ./config.sql address is incorrect!
just use
$ psql -U <user_name>
postgres=# \i config.sql
man sudo tells you:
-i, --login
Run the shell specified by the target user's password database entry as a login shell. This means that login-specific
resource files such as .profile, .bash_profile or .login will be read by the shell. If a command is specified, it is passed
to the shell for execution via the shell's -c option.
In particular, that will set your current working directory to the home directory of user postgres.
If you want to avoid that, don't use '-i'.

how to avoid postgresql backup content echo to screen

When I using this command to backup postgres database,the backup data echo to screen:
screen /usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
How to avoid it?When using this command :
/usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
This only echo backup log,do not contains content.
this is probably a bit late but there's an 'f' flag for this:
-f file
--file=file
Send output to the specified file. This parameter can be omitted for file based output formats, in which case the standard output is used. It must be given for the directory output format however, where it specifies the target directory instead of a file. In this case the directory is created by pg_dump and must not exist before.
So you can use it like this:
screen /usr/pgsql-9.6/bin/pg_dump -v -f dolphin-fulldb-backup-201904130913.bak -h prod-book-db -U postgres dolphin

How to import a sample DB into postgres?

According to a website I can download their sample file dvdrental.zip, but
The database file is in zipformat ( dvdrental.zip) so you need to extract > it to dvdrental.tar
First of all, what is a tar? I thought it had to be tar.gz to be compressed? I don't even know how to create a "tar" by itself. I tried:
tar -zcvf dvdrental.tar.gz dvdrental
and
tar -cf dvdrental.tar dvdrental
I try to import with pgAdmin 4 and I get either:
pg_restore: [archiver] input file does not appear to be a valid archive
or
pg_restore: [tar archiver] could not find header for file "toc.dat" in tar archive
respectively. Now, don't ask me why a popular tutorial site created a file in the wrong format. But, can you tell me how to repackage this file so I can use it as a sample DB?
Using Mac OS 10.12.4. Postgres 9.6. And PgAdmin 4 (not sure if it's in beta? It crashed and does all kinds of nonsensical window movement and highlighting)
I have extracted .zip archive first. Then opened pgAdmin and followed the guide "Load the DVD Rental database using the pgAdmin"
https://www.postgresqltutorial.com/load-postgresql-sample-database/
Pay attention to changing 'Format' field from 'Custom or Tar' to 'Directory'. Then you should be able to restore DB.
If you look into the .tar archive you will find the restore.sql where at the top:
-- File paths need to be edited. Search for $$PATH$$ and
-- replace it with the path to the directory containing
-- the extracted data files.
So to create sample DB you could to extract .tar content somewhere and use single command:
sed -e 's/\$\$PATH\$\$/\/path\/to\/extracted\/files/g' restore.sql | psql
Or
sed -e 's/\$\$PATH\$\$/\/path\/to\/extracted\/files/g' restore.sql > r.sql
and try to execute the r.sql content using PgAdmin.
get sample dataset from the link you cited and save somewhere.
Assuming postgres is installed and running do the following:
Run createdb dvdrental
Run pg_restore -d dvdrental ./dvdrental where "./dvdrental" is the path to the downloaded and unzipped file.
For create sample DB in postgres you following this steps:
1.- Create directory and enter it:
mkdir -p /tmp/dvdrental && cd /tmp/dvdrental
2.- Download zip file dvdrental.zip:
wget https://www.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip
3.- Uncompress file .zip and later .tar:
unzip dvdrental.zip
tar -xvf dvdrental.tar
4.- Replace in execution time $$PATH variable and review it with grep command:
sed -e 's/\$\$PATH\$\$/\/tmp\/dvdrental/g' restore.sql | grep --color dvdrental
5.- Import DB sample for specific host (localhost), port (5433), user (db) and database name (postgres):
sed -e 's/\$\$PATH\$\$/\/tmp\/dvdrental/g' restore.sql | psql -h localhost -p 5433 -U db -d postgres
Finally, I show import successful with program pgAdmin III

PostgreSQL: Exporting a database on Windows?

I need to export a database I created to get the code for creating the database and inserting rows.
I understand there is a method of using pg_dump, but all the walkthroughs of using it I can find seem to be on Linux.
Can anyone tell me how to do this on Windows?
You have to execute pg_dump located in the bin folder of your PostgreSQL install.
Ex : C:\Program Files\PostgreSQL\9.4\bin.
The command is pg_dump -U *username* -p *port* -d *database* -W -f *filename*
All the parameters are case sensitive ! (Check your username !)
U is for specifying the user that will connect to the DB.
If you don't specify it, pg_dump will use the login you're logged on with.
p for the port. (Default is 5432)
d for the database name
W to force pg_dump to ask for password
f the name of the file where the export should be stored. If you don't specify this, the dump will be displayed in the console.
Example :
pg_dump -U postgres -p 5432 -d postgres -W -f c:\vm\dump.sql
You may need special permissions to export the file to some folders.
(i.e. : C:\program files\ requires administrative rights for writing.)

how to backup and restore postgresql data from remote to local system in single command

I have to take database dump from remote server but i want to download it on my local system and then restore it directly on my local system with using a single command. actually my remote server disk is full so i can't take data bakup there.
You can use a pipe for this:
pg_dump -f - ... | psql -f - ...
or:
pg_dump -Fc -f - ... | pg_restore -f - ....
The -f - parameter (which is actually the default for pg_dump, but included for clarity) tells the command to write to stdout / read from stdin as appropriate.
Install pg_dump in one of your local system and run the following command from your local system
pg_dump -h your_remort_syten's_ip -U username database_name > /home/user/Desktop/dump_db.sql
I think this will help u.