community.postgresql.postgresql_user module – Create, alter, or remove a user (role) from a PostgreSQL server instance

Note

This module is part of the community.postgresql collection (version 3.9.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.postgresql. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: community.postgresql.postgresql_user.

Synopsis

  • Creates, alters, or removes a user (role) from a PostgreSQL server instance (“cluster” in PostgreSQL terminology) and, optionally, grants the user access to an existing database or tables.

  • A user is a role with login privilege.

  • You can also use it to grant or revoke user’s privileges in a particular database.

  • You cannot remove a user while it still has any privileges granted to it in any database.

  • Set fail_on_user to false to make the module ignore failures when trying to remove a user. In this case, the module reports if changes happened as usual and separately reports whether the user has been removed or not.

  • WARNING The priv option has been deprecated and will be removed in community.postgresql 4.0.0. Please use the community.postgresql.postgresql_privs module instead.

Requirements

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

  • psycopg2 >= 2.5.1

Parameters

Parameter

Comments

ca_cert

aliases: ssl_rootcert

string

Specifies the name of a file containing SSL certificate authority (CA) certificate(s).

If the file exists, verifies that the server’s certificate is signed by one of these authorities.

comment

string

added in community.postgresql 0.2.0

Adds a comment on the user (equivalent to the COMMENT ON ROLE statement).

To reset the comment, pass an empty string.

configuration

dictionary

added in community.postgresql 3.5.0

Role-specific configuration parameters that would otherwise be set by ALTER ROLE user SET variable TO value;.

Takes a dict where the key is the name of the configuration parameter. If the key includes special characters like . and -, it needs to be quoted to ensure the YAML is valid.

Sets or updates any parameter in the list that is not present or has the wrong value in the database.

Removes any parameter from the user that is not listed here.

Parameters that are present in the database but are not included in this list will only be reset, if reset_unspecified_configuration=true.

Inputs to user as well as keys and values in this parameter are quoted by the module. If you require the user to contain a ", you need to double it, otherwise the module will fail. " and ' are not allowed in configuration keys and any ' in the value of a configuration will be escaped by this module. Additionally, parameters and values are checked if trust_input is false.

Default: {}

conn_limit

integer

Specifies the user (role) connection limit.

connect_params

dictionary

added in community.postgresql 2.3.0

Any additional parameters to be passed to libpg.

These parameters take precedence.

Default: {}

db

aliases: login_db

string

Name of database to connect to and where user’s permissions are granted.

Default: ""

encrypted

boolean

Whether the password is stored hashed in the database.

You can specify an unhashed password, and PostgreSQL ensures the stored password is hashed when encrypted=true is set. If you specify a hashed password, the module uses it as-is, regardless of the setting of encrypted.

Note: Postgresql 10 and newer does not support unhashed passwords.

Previous to Ansible 2.6, this was false by default.

Choices:

  • false

  • true ← (default)

expires

string

The date at which the user’s password is to expire.

If set to 'infinity', user’s password never expires.

Note that this value must be a valid SQL date and time type.

fail_on_user

aliases: fail_on_role

boolean

If true, fails when the user (role) cannot be removed. Otherwise just log and continue.

Choices:

  • false

  • true ← (default)

login_host

aliases: host

string

Host running the database.

If you have connection issues when using localhost, try to use 127.0.0.1 instead.

Default: ""

login_password

string

The password this module should use to establish its PostgreSQL session.

Default: ""

login_unix_socket

aliases: unix_socket

string

Path to a Unix domain socket for local connections.

Default: ""

login_user

aliases: login

string

The username this module should use to establish its PostgreSQL session.

Default: "postgres"

name

aliases: user

string / required

Name of the user (role) to add or remove.

no_password_changes

boolean

If true, does not inspect the database for password changes. If the user already exists, skips all password related checks. Useful when pg_authid is not accessible (such as in AWS RDS). Otherwise, makes password changes as necessary.

Choices:

  • false ← (default)

  • true

password

string

Set the user’s password, before 1.4 this was required.

Password can be passed unhashed or hashed (MD5-hashed).

An unhashed password is automatically hashed when saved into the database if encrypted is set, otherwise it is saved in plain text format.

When passing an MD5-hashed password, you must generate it 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 | awk '{print $1}'`".

Note that if the provided password string is already in MD5-hashed format, then it is used as-is, regardless of encrypted option.

port

aliases: login_port

integer

Database port to connect to.

Default: 5432

priv

string

This option has been deprecated and will be removed in community.postgresql 4.0.0. Please use the community.postgresql.postgresql_privs module to GRANT/REVOKE permissions instead.

Slash-separated PostgreSQL privileges string: priv1/priv2, where you can define the user’s privileges for the database ( allowed options - ‘CREATE’, ‘CONNECT’, ‘TEMPORARY’, ‘TEMP’, ‘ALL’. For example CONNECT ) or for table ( allowed options - ‘SELECT’, ‘INSERT’, ‘UPDATE’, ‘DELETE’, ‘TRUNCATE’, ‘REFERENCES’, ‘TRIGGER’, ‘ALL’. For example table:SELECT ). Mixed example of this string: CONNECT/CREATE/table1:SELECT/table2:INSERT.

When priv contains tables, the module uses the schema public by default. If you need to specify a different schema, use the schema_name.table_name notation, for example, pg_catalog.pg_stat_database:SELECT.

reset_unspecified_configuration

boolean

added in community.postgresql 3.5.0

If set to true, the user’s default configuration parameters will be reset in case they are not included in configuration, otherwise existing parameters will not be modified if not included in configuration.

Choices:

  • false ← (default)

  • true

role_attr_flags

string

PostgreSQL user attributes string in the format: CREATEDB,CREATEROLE,SUPERUSER.

Note that ‘[NO]CREATEUSER’ is deprecated.

To create a simple role for using it like a group, use NOLOGIN flag.

See the full list of supported flags in documentation for your PostgreSQL version.

Default: ""

session_role

string

Switch to session role after connecting.

The specified session role must be a role that the current login_user is a member of.

Permissions checking for SQL commands is carried out as though the session role were the one that had logged in originally.

ssl_cert

path

added in community.postgresql 2.4.0

Specifies the file name of the client SSL certificate.

ssl_key

path

added in community.postgresql 2.4.0

Specifies the location for the secret key used for the client certificate.

ssl_mode

string

Determines how an SSL session is negotiated with the server.

See https://www.postgresql.org/docs/current/static/libpq-ssl.html for more information on the modes.

Default of prefer matches libpq default.

Choices:

  • "allow"

  • "disable"

  • "prefer" ← (default)

  • "require"

  • "verify-ca"

  • "verify-full"

state

string

The user (role) state.

Choices:

  • "absent"

  • "present" ← (default)

trust_input

boolean

added in community.postgresql 0.2.0

If false, checks whether values of options name, password, privs, expires, role_attr_flags, comment, session_role are potentially dangerous.

It makes sense to use false only when SQL injections through the options are possible.

Choices:

  • false

  • true ← (default)

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target.

Notes

Note

  • The module creates a user (role) with login privilege by default. Use NOLOGIN role_attr_flags to change this behaviour.

  • If you specify PUBLIC as the user (role), then the privilege changes apply to all users (roles). You may not specify password or role_attr_flags when the PUBLIC user is specified.

  • SCRAM-SHA-256-hashed passwords (SASL Authentication) require PostgreSQL version 10 or newer. On the previous versions the whole hashed string is used as a password.

  • Working with SCRAM-SHA-256-hashed passwords, be sure you use the environment: variable PGOPTIONS: "-c password_encryption=scram-sha-256" when it is not default for your PostgreSQL version (see the provided example).

  • On some systems (such as AWS RDS), pg_authid is not accessible, thus, the module cannot compare the current and desired password. In this case, the module assumes that the passwords are different and changes it reporting that the state has been changed. To skip all password related checks for existing users, use no_password_changes=true.

  • On some systems (such as AWS RDS), SUPERUSER is unavailable. This means the SUPERUSER and NOSUPERUSER role_attr_flags should not be specified to preserve idempotency and avoid InsufficientPrivilege errors.

  • The default authentication assumes that you are either logging in as or sudo’ing to the postgres account on the host.

  • To avoid “Peer authentication failed for user postgres” error, use postgres user as a become_user.

  • This module uses psycopg, a Python PostgreSQL database adapter. You must ensure that psycopg2 >= 2.5.1 or psycopg3 >= 3.1.8 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 python3-psycopg2 packages on the remote host before using this module.

See Also

See also

community.postgresql.postgresql_privs

Grant or revoke privileges on PostgreSQL database objects.

community.postgresql.postgresql_membership

Add or remove PostgreSQL roles from groups.

community.postgresql.postgresql_owner

Change an owner of PostgreSQL database object.

PostgreSQL database roles

Complete reference of the PostgreSQL database roles documentation.

PostgreSQL SASL Authentication

Complete reference of the PostgreSQL SASL Authentication.

Examples

# This example uses the 'priv' argument which is deprecated.
# You should use the 'postgresql_privs' module instead.
- name: Connect to acme database, create django user, and grant access to database and products table
  community.postgresql.postgresql_user:
    db: acme
    name: django
    password: ceec4eif7ya
    priv: "CONNECT/products:ALL"
    expires: "Jan 31 2020"

- name: Add a comment on django user
  community.postgresql.postgresql_user:
    db: acme
    name: django
    comment: This is a test user

# Connect to default database, create rails user, set its password (MD5- or SHA256-hashed),
# and grant privilege to create other databases and demote rails from super user status if user exists
# the hash from the corresponding pg_authid entry.
- name: Create rails user, set MD5-hashed password, grant privs
  community.postgresql.postgresql_user:
    name: rails
    password: md59543f1d82624df2b31672ec0f7050460
    # password: SCRAM-SHA-256$4096:zFuajwIVdli9mK=NJkcv1Q++$JC4gWIrEHmF6sqRbEiZw5FFW45HUPrpVzNdoM72o730+;fqA4vLN3mCZGbhcbQyvNYY7anCrUTsem1eCh/4YA94=
    role_attr_flags: CREATEDB,NOSUPERUSER
  # When using sha256-hashed password:
  #environment:
  #  PGOPTIONS: "-c password_encryption=scram-sha-256"

# This example uses the 'priv' argument which is deprecated.
# You should use the 'postgresql_privs' module instead.
- name: Connect to acme database and remove test user privileges from there
  community.postgresql.postgresql_user:
    db: acme
    name: test
    priv: "ALL/products:ALL"
    state: absent
    fail_on_user: false

# This example uses the 'priv' argument which is deprecated.
# You should use the 'postgresql_privs' module instead.
- name: Connect to test database, remove test user from cluster
  community.postgresql.postgresql_user:
    db: test
    name: test
    priv: ALL
    state: absent

# This example uses the 'priv' argument which is deprecated.
# You should use the 'postgresql_privs' module instead.
- name: Connect to acme database and set user's password with no expire date
  community.postgresql.postgresql_user:
    db: acme
    name: django
    password: mysupersecretword
    priv: "CONNECT/products:ALL"
    expires: infinity

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

- name: Connect to test database and remove an existing user's password
  community.postgresql.postgresql_user:
    db: test
    user: test
    password: ""

# Create user with a cleartext password if it does not exist or update its password.
# The password will be encrypted with SCRAM algorithm (available since PostgreSQL 10)
- name: Create appclient user with SCRAM-hashed password
  community.postgresql.postgresql_user:
    name: appclient
    password: "secret123"
  environment:
    PGOPTIONS: "-c password_encryption=scram-sha-256"

# This example uses the 'priv' argument which is deprecated.
# You should use the 'postgresql_privs' module instead.
- name: Create a user, grant SELECT on pg_catalog.pg_stat_database
  community.postgresql.postgresql_user:
    name: monitoring
    priv: 'pg_catalog.pg_stat_database:SELECT'

# Create a user and set a default-configuration that is active when they start a session
- name: Create a user with config-parameter
  community.postgresql.postgresql_user:
    name: appclient
    password: "secret123"
    configuration:
      work_mem: "16MB"

# Make sure user has only specified default configuration parameters
- name: Clear all configuration that is not explicitly defined for user
  community.postgresql.postgresql_user:
    name: appclient
    password: "secret123"
    configuration:
      work_mem: "16MB"
    reset_unspecified_configuration: true

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

queries

list / elements=string

List of executed queries.

Returned: success

Sample: ["CREATE USER \"alice\"", "GRANT CONNECT ON DATABASE \"acme\" TO \"alice\""]

Authors

  • Ansible Core Team