title: "Script Hooks: How to Protect PostgreSQL with Retrospect"
created_at: 2017.05.18
updated_at: 2017.09.05
category: Hooks
---

 
PostgreSQL is an free open source relational database management system (DBMS).PostgreSQL supports transactions, subselects, triggers, views, foreign key referential integrity, and sophisticated locking. PostgreSQL is the first database management system that implements multi-version concurrency control (MVCC) feature, even before Oracle. The MMVC feature is known as snapshot isolation in Oracle. PostgreSQL requires very minimum maintained efforts because of its stability.  Therefore, if you develop applications based on PostgreSQL, the total cost of ownership is low in comparison with other database management systems.
 
With Retrospect, you can protect your live, running PostgreSQL instance with no downtime. We’ll walk through how to do it.
 
By default, PostgreSQL stores data at /var/lib/postgresql/main/C:\Program Files\PostgreSQL\9.5\data\postgresql.confshow data_directory;
 
To protect the underlying data while your PostgreSQL instance is running, we will use Retrospect’s Script Hooks to mark the database fore backup before the backup using the StartSourceEndSource
 
Backup Steps
To protect PostgreSQL, Retrospect or Retrospect Client needs to be running on the same system.
 
- 
Download Retrospect_for_Windows_External_Scripts.zip and place the PostgreSQL script hook at the appropriate location: 
- 
Retrospect for Windows: C:\ProgramData\Retrospect
 
- 
Retrospect for Mac: /Library/Application Support/Retrospect
 
- 
Retrospect Client for Windows: C:\Program Files\Retrospect\Retrospect Client
 
- 
Retrospect Client for Mac: /Library/Application Support/Retrospect Client/retroeventhandler
 
 
 
- 
In the RetroEventHandler
 
function StartSource {
    sudo -u postgres /usr/local/bin/psql -c "select pg_start_backup('retrospect_backup', true);" -q &>/dev/null
    echo
}
function EndSource {
    sudo -u postgres /usr/local/bin/psql -c "select pg_stop_backup();" -q &>/dev/null
    echo
}
 
 
- 
Start a backup of the system where PostgreSQL is running. You do not need to restart Retrospect for the updated file to be used. 
 
 
Restore Steps
- 
Stop your PostgreSQL instance. 
- 
Restore the Retrospect backup containing the PostgreSQL data store. 
- 
Start your PostgreSQL instance. The instance will utilize the restored data.