• Docs »
  • postgresql_user - Adds or removes a users (roles) from a PostgreSQL database.

postgresql_user - Adds or removes a users (roles) from a PostgreSQL database.


  • Add or remove PostgreSQL users (roles) from a remote host and, optionally, grant the users access to an existing database or tables.
  • The fundamental function of the module is to create, or delete, roles from a PostgreSQL cluster. Privilege assignment, or removal, is an optional step, which works on one database at a time. This allows for the module to be called several times in the same module to modify the permissions on different databases, or to grant permissions to already existing users.
  • A user cannot be removed until all the privileges have been stripped from the user. In such situation, if the module tries to remove the user it will fail. To avoid this from happening the fail_on_user option signals the module to try to remove the user, but if not possible keep going; the module will report if changes happened and separately if the user was removed or not.


The below requirements are needed on the host that executes this module.

  • psycopg2


Parameter Choices/Defaults Comments
(added in 2.4)
Specifies the user connection limit.
Name of database where permissions will be granted.

(added in 1.4)
  • no
  • yes ←
Whether the password is stored hashed in the database. Passwords can be passed already hashed or unhashed, and postgresql ensures the stored password is hashed when encrypted is set.
Note: Postgresql 10 and newer doesn't support unhashed passwords.
Previous to Ansible 2.6, this was no by default.
(added in 1.4)
The date at which the user's password is to expire.
If set to 'infinity', user's password never expire.
Note that this value should be a valid SQL date and time type.
  • no
  • yes ←
If yes, fail when user can't be removed. Otherwise just log and continue.
login_host Default:
Host running PostgreSQL.
Password used to authenticate with PostgreSQL.
Path to a Unix domain socket for local connections.
login_user Default:
User (role) used to authenticate with PostgreSQL.
Name of the user (role) to add or remove.

(added in 2.0)
  • no ←
  • yes
If yes, don't inspect database for password changes. Effective when pg_authid is not accessible (such as AWS RDS). Otherwise, make password changes as necessary.
Set the user's password, before 1.4 this was required.
Password can be passed unhashed or hashed (MD5-hashed).
Unhashed password will automatically be hashed when saved into the database if encrypted parameter is set, otherwise it will be save in plain text format.
When passing a hashed password it must be generated with the format 'str["md5"] + md5[ password + username ]', resulting in a total of 35 characters. An easy way to do this is echo "md5$(echo -n 'verysecretpasswordJOE' | md5sum").
Note that if the provided password string is already in MD5-hashed format, then it is used as-is, regardless of encrypted parameter.
port Default:
Database port to connect to.
PostgreSQL privileges string in the format: table:priv1,priv2.
PostgreSQL role attributes string in the format: CREATEDB,CREATEROLE,SUPERUSER.
Note that '[NO]CREATEUSER' is deprecated.
(added in 2.3)
  • disable
  • allow
  • prefer ←
  • require
  • verify-ca
  • verify-full
Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.
Default of prefer matches libpq default.
(added in 2.3)
Specifies the name of a file containing SSL certificate authority (CA) certificate(s). If the file exists, the server's certificate will be verified to be signed by one of these authorities.
  • present ←
  • absent
The user (role) state.



  • The default authentication assumes that you are either logging in as or sudo’ing to the postgres account on the host.
  • This module uses psycopg2, a Python PostgreSQL database adapter. You must ensure that psycopg2 is installed on the host before using this module. If the remote host is the PostgreSQL server (which is the default case), then PostgreSQL must also be installed on the remote host. For Ubuntu-based systems, install the postgresql, libpq-dev, and python-psycopg2 packages on the remote host before using this module.
  • If you specify PUBLIC as the user, then the privilege changes will apply to all users. You may not specify password or role_attr_flags when the PUBLIC user is specified.
  • The ssl_rootcert parameter requires at least Postgres version 8.4 and psycopg2 version 2.4.3.


# Create django user and grant access to database and products table
- postgresql_user:
    db: acme
    name: django
    password: ceec4eif7ya
    priv: "CONNECT/products:ALL"

# Create rails user, set its password (MD5-hashed) and grant privilege to create other
# databases and demote rails from super user status
- postgresql_user:
    name: rails
    password: md59543f1d82624df2b31672ec0f7050460
    role_attr_flags: CREATEDB,NOSUPERUSER

# Remove test user privileges from acme
- postgresql_user:
    db: acme
    name: test
    priv: "ALL/products:ALL"
    state: absent
    fail_on_user: no

# Remove test user from test database and the cluster
- postgresql_user:
    db: test
    name: test
    priv: ALL
    state: absent

# Set user's password with no expire date
- postgresql_user:
    db: acme
    name: django
    password: mysupersecretword
    priv: "CONNECT/products:ALL"
    expire: infinity

# Example privileges string format
# INSERT,UPDATE/table:SELECT/anothertable:ALL

# Remove an existing user's password
- postgresql_user:
    db: test
    user: test
    password: ""


This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.


This module is flagged as community which means that it is maintained by the Ansible Community. See Module Maintenance & Support for more info.

For a list of other modules that are also maintained by the Ansible Community, see here.


  • Ansible Core Team


If you notice any issues in this documentation you can edit this document to improve it.