Documentation

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

Synopsis

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.

Requirements (on host that executes module)

  • psycopg2

Options

parameter required default choices comments
db
no
    name of database where permissions will be granted
    encrypted
    (added in 1.4)
    no
      whether the password is stored hashed in the database. boolean. Passwords can be passed already hashed or unhashed, and postgresql ensures the stored password is hashed when encrypted is set.
      expires
      (added in 1.4)
      no
        sets the user's password expiration.
        fail_on_user
        no yes
        • yes
        • no
        if yes, fail when user can't be removed. Otherwise just log and continue
        login_host
        no localhost
          Host running PostgreSQL.
          login_password
          no
            Password used to authenticate with PostgreSQL
            login_unix_socket
            no
              Path to a Unix domain socket for local connections
              login_user
              no postgres
                User (role) used to authenticate with PostgreSQL
                name
                yes
                  name of the user (role) to add or remove
                  no_password_changes
                  (added in 2.0)
                  no no
                  • yes
                  • no
                  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.
                  password
                  no
                    set the user's password, before 1.4 this was required.
                    When passing an encrypted password, the encrypted parameter must also be true, and 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" | md5`". Note that if encrypted is set, the stored password will be hashed whether or not it is pre-encrypted.
                    port
                    no 5432
                      Database port to connect to.
                      priv
                      no
                        PostgreSQL privileges string in the format: table:priv1,priv2
                        role_attr_flags
                        no
                        • [NO]SUPERUSER
                        • [NO]CREATEROLE
                        • [NO]CREATEUSER
                        • [NO]CREATEDB
                        • [NO]INHERIT
                        • [NO]LOGIN
                        • [NO]REPLICATION
                        PostgreSQL role attributes string in the format: CREATEDB,CREATEROLE,SUPERUSER
                        state
                        no present
                        • present
                        • absent
                        The user (role) state

                        Examples

                        # 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, grant privilege to create other databases and demote rails from super user status
                        - postgresql_user: name=rails password=secret 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
                        
                        # Example privileges string format
                        INSERT,UPDATE/table:SELECT/anothertable:ALL
                        
                        # Remove an existing user's password
                        - postgresql_user: db=test user=test password=NULL
                        

                        Notes

                        Note

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

                        Note

                        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.

                        Note

                        If the passlib library is installed, then passwords that are encrypted in the DB but not encrypted when passed as arguments can be checked for changes. If the passlib library is not installed, unencrypted passwords stored in the DB encrypted will be assumed to have changed.

                        Note

                        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.

                        This is a Core Module

                        For more information on what this means please read Core Modules

                        For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Helping Testing PRs and Developing Modules.