automatic backup in postgresql creates the empty backup - postgresql

I want to automate backup of PostgreSQL database using crontab in UNIX. I have tried but it will create 0 bytes backup.
My crontab entry is:
24 * * * * /home/desktop/myscript.sh
and my sh file contains the following code:
pg_dump -U PostgreSQL -d test > b.backup
It will create the file but the file is empty. Is there any solution? Is there any way to solve this question?

Don't assume that any environment variables are set in a cron job; be explicit:
/full/path/to/pg_dump -U postgres -d test > /full/path/to/b.backup
Look for mail in your inbox for failure reports from cron.

You must specify full path to pg_dump
#!/bin/bash
BKPDATE=$(date +%d.%m.%Y-%H:%M:%S)
cd /var/lib/pgsql/12/backups
/usr/pgsql-12/bin/pg_dump -Fc dl_db > DBNAME_$BKPDATE.dmp --verbose 2> LOG_$BKPDATE.log
or you must add PostgreSQL's bin directory to the path like below:
vi /var/lib/pgsql/.pgsql_profile
export PATH=$PATH:/usr/pgsql-12/bin

Related

PostgreSQL DB dump location

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

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 Database export to .sql file

I want to export my database as a .sql file.
Can someone help me? The solutions I have found don't work.
A detailed description please.
On Windows 7.
pg_dump defaults to plain SQL export. both data and structure.
open command prompt and
run pg_dump -U username -h localhost databasename >> sqlfile.sql
Above command is preferable as most of the times there will be an error which will be something like - ...FATAL: Peer authentication failed for user ...
In windows, first, make sure the path is added in environment variables PATH
C:\Program Files\PostgreSQL\12\bin
After a successful path adding restart cmd and type command
pg_dump -U username -p portnumber -d dbname -W -f location
this command will export both schema and data
for only schema use -s in place of -W
and for only data use -a.
replace each variable like username, portnumber, dbname and location according to your situation
everything is case sensitive, make sure you insert everything correctly,
and to import
psql -h hostname -p port_number -U username -f your_file.sql databasename
make sure your db is created or creation query is present in .sql file
Documentation: https://www.postgresql.org/docs/current/app-pgdump.html
Go to your command line and run
pg_dump -U userName -h localhost -d databaseName > ~/Desktop/cmsdump.sql

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.

How to import existing *.sql files in PostgreSQL 8.4?

I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?
From the command line:
psql -f 1.sql
psql -f 2.sql
From the psql prompt:
\i 1.sql
\i 2.sql
Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:
for f in *.sql ; do psql -f $f ; done
Here's the documentation of the psql application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html
in command line first reach the directory where psql is present then write commands like this:
psql [database name] [username]
and then press enter psql asks for password give the user password:
then write
> \i [full path and file name with extension]
then press enter insertion done.
Well, the shortest way I know of, is following:
psql -U {user_name} -d {database_name} -f {file_path} -h {host_name}
database_name: Which database should you insert your file data in.
file_path: Absolute path to the file through which you want to perform the importing.
host_name: The name of the host. For development purposes, it is mostly localhost.
Upon entering this command in console, you will be prompted to enter your password.
Be careful with "/" and "\". Even on Windows the command should be in the form:
\i c:/1.sql
Always preferred using a connection service file (lookup/google 'psql connection service file')
Then simply:
psql service={yourservicename} < {myfile.sql}
Where yourservicename is a section name from the service file.
enter image description here
use following command :-
C:\Program Files\PostgreSQL\12\bin>psql -U username -d databasename -f D:\file.sql