To install PostgreSQL using Ansible, we need to create a playbook that automates the installation process. The playbook will target a group of machines (or a single machine) and install PostgreSQL on them.
Below are the detailed steps to install PostgreSQL using Ansible:
1. Prerequisites
Before you begin, ensure you have the following:
- Ansible installed on your control node (the machine from which you will run the playbook). Please refer https://www.revasys.com/2024/12/install-latest-ansible-on-ubuntu-24-04/
- SSH access to the target machines (hosts) where PostgreSQL will be installed.
- The target machines should be running a compatible Linux distribution (e.g., Ubuntu, CentOS, Debian).
- A configured inventory file in Ansible that lists the target machines.
2. Creating the Ansible Playbook
- Create a new playbook file called
install_postgresql.yml
(or another name of your choice). - Define the tasks for installing PostgreSQL.
Here’s an example playbook that installs PostgreSQL on an Ubuntu machine:
---
- name: Install PostgreSQL on Ubuntu
hosts: all
become: yes # Run with sudo privileges
tasks:
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600 # Cache validity in seconds
- name: Install PostgreSQL
apt:
name: postgresql
state: present
- name: Ensure PostgreSQL service is started
service:
name: postgresql
state: started
enabled: yes
Explanation of the Playbook:
hosts: all
: The playbook will run on all the hosts defined in the inventory file.become: yes
: It ensures the tasks are executed with root privileges (usingsudo
).apt
: The module for managing packages on Debian-based systems (like Ubuntu). It updates the cache and installs PostgreSQL.service
: Ensures that the PostgreSQL service is started and enabled to start on boot.
3. Inventory File
You need an inventory file to tell Ansible which hosts to target. Create a file called inventory.ini
(or any name you prefer).
Example inventory.ini
:
[postgresql_servers]
192.168.1.100 ansible_user=your_ssh_user
192.168.1.101 ansible_user=your_ssh_user
This inventory lists two servers (192.168.1.100
and 192.168.1.101
) under the postgresql_servers
group. Replace your_ssh_user
with the actual SSH username.
4. Run the Playbook
To execute the playbook, run the following command from your control node:
ansible-playbook -i inventory.ini install_postgresql.yml
5. For CentOS / RHEL / Amazon Linux
For CentOS-based distributions (like CentOS, RHEL, or Amazon Linux), the playbook would be slightly different, as it would use the yum
or dnf
package manager instead of apt
. Here is an example for CentOS:
---
- name: Install PostgreSQL on Redhat
hosts: all
become: yes
tasks:
- name: Install PostgreSQL repo key
yum:
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-42.0-46PGDG.noarch.rpm
state: present
disable_gpg_check: true
- name: Install PostgreSQL repo
yum:
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
state: present
- name: Install PostgreSQL
yum:
name: postgresql17-server
state: present
- name: Initialize PostgreSQL database
command: /usr/pgsql-17/bin/postgresql-17-setup initdb
args:
creates: /var/lib/pgsql/17/data/PG_VERSION
- name: Ensure PostgreSQL service is started
service:
name: postgresql-17
state: started
enabled: yes
In this playbook:
- We install the PostgreSQL repository first (
pgdg-redhat-repo-latest
). - The PostgreSQL 17 server is installed via
yum
. - The database is initialized using the
postgresql-17-setup
command. - Finally, we start and enable the PostgreSQL service.
6. Verify Installation
After running the playbook successfully, you can check if PostgreSQL is installed and running:
- SSH into the target machine.
- Run the command:
psql --version
This will output the installed PostgreSQL version, confirming that the installation was successful.
7. Additional Configuration (Optional)
After installation, you may need to configure PostgreSQL to:
- Set up a specific database or user.
- Configure remote access (allow connections from other machines).
- Set password authentication and other security settings.
You can create additional tasks in your Ansible playbook to configure PostgreSQL as required, for example:
- name: Set password for PostgreSQL user 'postgres'
postgresql_user:
name: postgres
password: "your_password"
state: present
This task uses the postgresql_user
Ansible module to set a password for the postgres
user.
Conclusion
By using Ansible, you can automate the installation and configuration of PostgreSQL across multiple servers, making it more efficient and repeatable. The playbook provided can be customized for different Linux distributions, PostgreSQL versions, and configurations, depending on your specific needs.