AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
community.postgresql.postgresql_user: Create & Manage PostgreSQL Users — Video Tutorial
How to create PostgreSQL users and roles with Ansible community.postgresql.postgresql_user module. Set passwords, grant privileges, manage roles. Practical YAML playbook examples.
What You'll Learn
- How to Create a PostgreSQL User / Role with Ansible?
- Ansible Create a PostgreSQL User/Role
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to Create a PostgreSQL User / Role 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 Create a PostgreSQL User/Role
- `community.postgresql.postgresql_user`
- Create, alter, or remove a user (role) from a PostgreSQL server instance
Let's talk about the Ansible module `postgresql_user`.
The full name is `community.postgresql.postgresql_user`, 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 create, alter, or remove a user (role) from a PostgreSQL server instance.
This module uses `psycopg2`, a Python PostgreSQL User library. You must ensure that `python3-psycopg2` is installed on the host before using this module.
Parameters
- name _string_ - Name of User
- state _string_ - present/absent - The user (role) state
- password _string_ - Password cleartext or MD5-hashed
- db _string_ - Grant user permission to the database
Let me summarize the main parameters of the module `postgresql_user`.
Ansible supposes that PostgreSQL is in the target node.
The only required parameter is `name`, the name of the user to interact with.
The parameter `state` specify the desired user (role) state. The option "present" means that the user/role should be created. The option `absent` means that the user/role should be deleted.
You could specify the desired password in the `password` parameter in cleartext or MD5-hashed format.
You could also specify a `database` parameter to specify the name of the database to connect to and where the user's permissions are granted. You could also perform the same operation using the `postgresql_privs` Ansible module.
Links
- [`community.postgresql.postgresql_user`](https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_user_module.html)
## Playbook
Let's jump into a real-life Ansible Playbook to Create a PostgreSQL User now called Role.
I'm going to show you how to create the `myuser` user (role) in the current PostgreSQL server.
code
```yaml
---
- name: postgresql Playbook
hosts: all
become: true
vars:
db_user: myuser
db_password: MySecretPassword123
tasks:
- name: Utility present
ansible.builtin.package:
name: python3-psycopg2
state: present
- name: Create db user
community.postgresql.postgresql_user:
state: present
name: "{{ db_user }}"
password: "{{ db_password }}"
become: true
become_user: postgres
```
execution
```bash
$ ansible-playbook -i virtualmachines/demo/inventory postgresql/user_create.yml
PLAY [postgresql Playbook] ************************************************************************************
TASK [Gathering Facts] *****************************
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: installation
Read the full written article: community.postgresql.postgresql_user: Create & Manage PostgreSQL Users
Related Video Tutorials
- Configure PostgreSQL with Ansible: User Access and Service Management — Learn how to automate PostgreSQL configuration with Ansible. This guide shows how to set user access with md5 authentication and manage PostgreSQL services.
- community.postgresql.postgresql_db: Create & Manage PostgreSQL Databases — How to create and manage PostgreSQL databases with Ansible community.postgresql.postgresql_db module. Set encoding, owner, template. Backup and restore databases. Practical examples.
- Automate PostgreSQL Backups with Ansible Playbook — Discover how to automate PostgreSQL database backups with Ansible. This guide includes a live Playbook example and detailed execution steps for seamless data management.
- Drop a PostgreSQL Database - Ansible module postgresql_db — How to automate the delete of a "testdb" database on PostgreSQL using Ansible Playbook and postgresql_db module.
- Grant Privileges to User/Role on PostgreSQL Database - Ansible module postgresql_privs — How to automate the granting of all permission for "myuser" user/role on database "testdb" on PostgreSQL using Ansible Playbook and postgresql_privs module.
- Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service — How to automate the installation of PostgreSQL on Debian-like systems: installing the necessary packages and dependency, initializing the configuration, starting and enabling service on boot using Ansible Playbook and apt, stat, shell, and service modules.