# Postgres - grant role to user

**URL:** <https://forum.ansible.com/t/postgres-grant-role-to-user/18628>\
**Category:** Ansible Project\
**Created:** [June 28, 2015, 12:39am UTC](https://forum.ansible.com/t/postgres-grant-role-to-user/18628 "2015-06-28T00:39:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Levesque](https://avatars.discourse-cdn.com/v4/letter/i/51bf81/32.png) [@Ian\_Levesque](https://forum.ansible.com/u/Ian_Levesque)\
**Post date:** [June 28, 2015, 12:39am UTC](https://forum.ansible.com/t/postgres-grant-role-to-user/18628/1 "2015-06-28T00:39:45Z")

</div>

Hi,

I’m trying to figure out if/how I can use the postgresql\_user module to grant a role to a user.

I thought I may be able to specify the role via role\_attr\_flags but that doesn’t work for granting a custom role.

Does anyone know how to do this?

Thanks,  
Ian

---

<div class="post-metadata">

**Author:** ![Ian\_Levesque](https://avatars.discourse-cdn.com/v4/letter/i/51bf81/32.png) [@Ian\_Levesque](https://forum.ansible.com/u/Ian_Levesque)\
**Post date:** [June 29, 2015, 3:18pm UTC](https://forum.ansible.com/t/postgres-grant-role-to-user/18628/2 "2015-06-29T15:18:33Z")

</div>

To answer my own question, you can assign membership to a role in postgres using the postgres\_privs module, using a syntax such as:

users:  
admin:  
password: “{{ encrypted\_password }}”  
roles: full,admin

employee:  
password: “{{ encrypted\_password }}”  
roles: readonly

- name: Grant users membership to their appropriate role  
postgresql\_privs:  
type: group  
db: database\_name  
port: 5432  
login\_host: [db.server.com](http://db.server.com)  
login\_user: admin\_user  
login\_password: “{{ encrypted\_admin\_password }}”  
roles: “{{ item.key }}”  
objs: “{{ item.value.roles }}”  
with\_dict: “{{ users }}”

Where “roles” is the comma-separated list of affected users and “objs” is the comma-separated list of roles to which the user(s) should be granted membership. The database “db” is only used for connection, since role membership is cluster-wide.

Best,  
Ian
