Search code examples
postgresqlsalt-project

Salt: execute custom SQL after creating a postgres user


Creating postgres users with salt works perfectly with states.postgres_user

I need to run a custom SQL to alter the user:

ALTER USER foo_bar_p CONNECTION LIMIT 20;

How can I run the above SQL after salt created the user foo_bar_p?


Solution

  • The following is untested, but I believe it will get you there:

    postgres-set-user-conn-limit:
      cmd.run:
        - name: psql -U <user> -d <db> -c 'ALTER USER foo_bar_p CONNECTION LIMIT 20;'
        - env: PGPASSWORD='<password>'
        - require:
          - postgres_user: foo_bar_p
    

    Or as @guettli suggested in the comments, a method to avoid requiring a password at all:

    postgres-set-user-conn-limit:
      cmd.run:
        - name: psql -U <user> -d <db> -c 'ALTER USER foo_bar_p CONNECTION LIMIT 20;'
        - user: postgres
        - require:
          - postgres_user: foo_bar_p