Restore a PostgreSQL Database — Ansible module postgresql_db
By Luca Berton · Published 2024-01-01 · Category: installation
How to automate the restore of a backup file of a “testdb” database on PostgreSQL using Ansible Playbook and postgresql_db module.
How to Restore a PostgreSQL Database with Ansible? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Restore a PostgreSQL Database • community.postgresql.postgresql_db • Add or remove PostgreSQL databases from a remote host
Let's talk about the Ansible module postgresql_db. The full name is community.postgresql.postgresql_db, which means that is part of the collection of modules "community.postgresql" maintained by the Ansible Community to interact with PostgreSQL. The collection is tested with ansible-core version 2.11+, prior versions such as 2.9 or 2.10 are not supported. The purpose of the module is to add or remove PostgreSQL databases from a remote host.
Parameters • name _string_ - Name of database • state _string_ - present/absent/dump/restore/rename - The operation • targer _string_ - filename
Let me summarize the main parameters of the module postgresql_db. Ansible supposes that PostgreSQL is in the target node. The only required parameter is "name", the name of the database to interact with. The parameter "state" specifies the desired state or the operation for the selected database. The option "present" means that the database should be created and the option "absent" means that the database should be deleted. Other useful operations are "dump" and "restore" which use pg_dump, the embedded PostgreSQL utility to backup and restore to the target file. Another useful operation is rename, from name to target. This module use psycopg2, a Python PostgreSQL database library. You must ensure that python3-psycopg2 is installed on the host before using this module.
Links • community.postgresql.postgresql_db
## Playbook Let's jump into a real-life Ansible Playbook to Restore a PostgreSQL Database. I'm going to show you how to restore a backup file stored in /backups/testdb.gzin the testdb database in the current PostgreSQL server.
code
execution
NOT idempotency
before execution
after execution
Conclusion
Now you know how to automate the Restore of a backup in a PostgreSQL Database with Ansible.
Related Articles • Ansible Template Guide • Ansible Become Guide • Ansible Inventory Guide • ansible.builtin.file Guide
Category: installation
Watch the video: Restore a PostgreSQL Database — Ansible module postgresql_db — Video Tutorial