# How to execute SQL query on PostgreSQL?

**URL:** <https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050>\
**Category:** Ansible Project\
**Created:** [November 30, 2021, 3:48pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050 "2021-11-30T15:48:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Huesca](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@Daniel\_Huesca](https://forum.ansible.com/u/Daniel_Huesca)\
**Post date:** [November 30, 2021, 3:48pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050/1 "2021-11-30T15:48:39Z")

</div>

I’m reading the docs @ [community.postgresql.postgresql\_query module – Run PostgreSQL queries — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_query_module.html) but I keep getting errors when trying to execute a few simple commands…

 ![Captura.PNG](https://us1.discourse-cdn.com/ansible/original/2X/7/71424486827b0bd196be2488f5f3d12a67867c53.png)

This is the output I’m getting

 ![Captura2.PNG](https://us1.discourse-cdn.com/ansible/original/2X/a/a972ea2107a32dcbf7cfbd46b5a6b0068c4846b5.png)

Any idea on what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 30, 2021, 3:59pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050/2 "2021-11-30T15:59:21Z")

</div>

Not posting text, but pixely/hard-to-read images, for starters...

---

<div class="post-metadata">

**Author:** ![Daniel\_Huesca](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@Daniel\_Huesca](https://forum.ansible.com/u/Daniel_Huesca)\
**Post date:** [November 30, 2021, 4:13pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050/3 "2021-11-30T16:13:38Z")

</div>

This is the code I’m using:

- name: DELETE POSTGRESQL ROLE  
community.postgresql.postgresql\_query:  
login\_host: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
db: db\_test  
login\_user: master  
login\_password: 0123456789  
query: GRANT %(role)s TO %(admin)s; REASSIGN OWNED BY %(role)s TO %(admin)s; DROP OWNED BY %(role)s; DROP ROLE %(role)s;  
named\_args:  
role: delete-user  
admin: master

This is the console output:  
fatal: [localhost]: FAILED! =\> {“changed”: false, “msg”: “Cannot execute SQL ‘GRANT %(role)s TO %(admin)s; REASSIGN OWNED BY %(role)s TO %(admin)s; DROP OWNED BY %(role)s; DROP ROLE %(role)s;’ {‘role’: ‘delete-user’, ‘admin’: ‘master’}: syntax error at or near "‘delete-user’"\nLINE 1: GRANT ‘delete-user’ TO ‘master’; REASSIGN OWNED BY 'delete-u…\n ^\n, query list: [‘GRANT %(role)s TO %(admin)s; REASSIGN OWNED BY %(role)s TO %(admin)s; DROP OWNED BY %(role)s; DROP ROLE %(role)s;’]”}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 30, 2021, 4:50pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050/4 "2021-11-30T16:50:43Z")

</div>

I see that you're supplying multiple SQL statements at the 'query' parameter.  
I don't know the module that well, but it could be that that is not supported?  
In that case, probably you could loop over a list of queries:

- name: DELETE POSTGRESQL ROLE  
&nbsp;&nbsp;community.postgresql.postgresql\_query:  
&nbsp;&nbsp;&nbsp;&nbsp;login\_host: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
&nbsp;&nbsp;&nbsp;&nbsp;db: db\_test  
&nbsp;&nbsp;&nbsp;&nbsp;login\_user: master  
&nbsp;&nbsp;&nbsp;&nbsp;login\_password: 0123456789  
&nbsp;&nbsp;&nbsp;&nbsp;query: GRANT %(role)s TO %(admin)s; REASSIGN OWNED BY %(role)s TO  
%(admin)s; DROP OWNED BY %(role)s; DROP ROLE %(role)s;  
&nbsp;&nbsp;&nbsp;&nbsp;named\_args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: delete-user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;admin: master  
&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;- GRANT %(role)s TO %(admin)s;  
&nbsp;&nbsp;&nbsp;- REASSIGN OWNED BY %(role)s TO %(admin)s;  
&nbsp;&nbsp;&nbsp;- DROP OWNED BY %(role)s;  
&nbsp;&nbsp;&nbsp;- DROP ROLE %(role)s;

(not tried, ymmv)

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [December 2, 2021, 3:35pm UTC](https://forum.ansible.com/t/how-to-execute-sql-query-on-postgresql/36050/5 "2021-12-02T15:35:15Z")

</div>

The SQL is not idempotent: It will fail if run again, and nothing is  
apparent in the code snippet to prevent ansible from trying to run it  
even if it's already succeeded. Check the status of the relevant SQL  
server to see whether the relevant roles and accounts exist now.
