---
title: Bash Script to Backup Remote Postgres Databases via Cron with Password Authentication
canonical: https://0110.be/posts/Bash_Script_to_Backup_Remote_Postgres_Databases_via_Cron_with_Password_Authentication
markdown_url: https://0110.be/posts/Bash_Script_to_Backup_Remote_Postgres_Databases_via_Cron_with_Password_Authentication.md
id: 307
published_at: '2008-07-17T00:00:00Z'
updated_at: '2013-12-05T18:19:15Z'
author: Joren
tags:
- name: 0110.be
  markdown_url: https://0110.be/tags/0110.be.md
- name: Code
  markdown_url: https://0110.be/tags/Code.md
---

# Bash Script to Backup Remote Postgres Databases via Cron with Password Authentication

![PostgreSQL Logo](https://0110.be/files/attachments/307/elephant-64.png "PostgreSQL Logo")

I have modified a bash-script to backup PostgreSQL databases, this is [the original script](http://archives.postgresql.org/pgsql-admin/2001-03/msg00143.php). The "modified version":\[database_backup_script.txt\] can be used to backup databases on a remote or local database server. Also this script does not need a trust relationship but uses a login and password. To get started you need to:

1.  Modify the directory and database variables to suit your needs.

2.  Add an entry to crontab to perform the backups nightly or whenever you wish.

3.  Have fun.

The script empties ~/.pgpass\ and\ writes\ login\ info\ for\ the\ system\ databases.\ Then\ it\ logs\ in\ and\ fetches\ an\ up-to-date\ list\ of\ databases.\ For\ every\ database\ an\ entry\ is\ made\ in~/.pgpass and every database is backed up. The results are logged to \$logfile.

