patternsqlMinor
PostgreSQL Scheduled Database Backup
Viewed 0 times
postgresqldatabasebackupscheduled
Problem
For scheduling a daily back up on a postgres database we execute the following command:
After the code executes, the log says "invalid password". Is there a way to specify a password or avoid the requirement when creating a dump?
Also we would like to retrieve the dump and pop it on separate Windows server using pgadmin3. When restoring using pgadmin
0 2 * * * /usr/local/pgsql/bin/pg_dump -Fp -b -U database_name \
> $HOME/db_backups/database_name-`date +\%Y\%m\%d`.sql \
2>> $HOME/db_backups/cron.logAfter the code executes, the log says "invalid password". Is there a way to specify a password or avoid the requirement when creating a dump?
Also we would like to retrieve the dump and pop it on separate Windows server using pgadmin3. When restoring using pgadmin
dbase -> database restore the 'OK' button is disabled and we are unable to restore the database.Solution
Backup without password
If you run the script as OS user
So make it a cronjob of postgres if you can. Or enable password-less access for the OS user running the job. You can do that in
More on how to connect without password in this related question.
Restore in pgAdmin
I quote the pgAdmin3 FAQ:
pgAdmin III uses PostgreSQL's pg_restore tool, which supports only the
COMRESS and TAR options of pg_dump which is used for backup creation.
The PLAIN format can't be interpreted by pgAdmin III and pg_restore
(it can be edited manually, and executed with psql and pgAdmin III's
query tool in many cases), and thus isn't accepted as valid file.
We recommend using the COMPRESS format for daily backup tasks. The
PLAIN format is for advanced manual processing before executing as SQL
script, and has some restrictions (no blobs) which makes it less
usable for standard backup tasks.
To restore a plain SQL backup execute it with psql similar to this:
For a single database backup you have to connect to the right database. For a cluster-backup with
You could also load and run such a backup with the pgAdmin query tool. It's plain SQL.
If you run the script as OS user
postgres it should not request a password in a standard setup, because password-less peer access (or ident on older versions) is enabled in pg_hba.conf for the postgres user.So make it a cronjob of postgres if you can. Or enable password-less access for the OS user running the job. You can do that in
pg_hba.conf but, as @Richard already pointed out, the password file .pgpass file may be a more elegant solution.More on how to connect without password in this related question.
Restore in pgAdmin
I quote the pgAdmin3 FAQ:
pgAdmin III uses PostgreSQL's pg_restore tool, which supports only the
COMRESS and TAR options of pg_dump which is used for backup creation.
The PLAIN format can't be interpreted by pgAdmin III and pg_restore
(it can be edited manually, and executed with psql and pgAdmin III's
query tool in many cases), and thus isn't accepted as valid file.
We recommend using the COMPRESS format for daily backup tasks. The
PLAIN format is for advanced manual processing before executing as SQL
script, and has some restrictions (no blobs) which makes it less
usable for standard backup tasks.
To restore a plain SQL backup execute it with psql similar to this:
psql mydb -p 5432 -U myuser -f backup.sqlFor a single database backup you have to connect to the right database. For a cluster-backup with
pg_dumpall, you might as well connect to the maintenance db "postgres".You could also load and run such a backup with the pgAdmin query tool. It's plain SQL.
Code Snippets
psql mydb -p 5432 -U myuser -f backup.sqlContext
StackExchange Database Administrators Q#14725, answer score: 2
Revisions (0)
No revisions yet.