-
Notifications
You must be signed in to change notification settings - Fork 2
postgresql_query
Denis Gasparin edited this page Oct 8, 2017
·
2 revisions
- execute a query in a PostGreSQL database and return the results
These are requirements in the host that executes the module:
- psycopg2
| parameter | required | default | choices | comments |
|---|---|---|---|---|
| database | no | postgres | Name of the database to connect to. | |
| login_host | no | localhost | Host running the database. | |
| login_user | no | The username used to authenticate with. | ||
| login_password | no | The password used to authenticate with. | ||
| login_unix_socket | no | Path to a Unix domain socket for local connections. | ||
| port | no | 5432 | Database port to connect to. | |
| query | yes | Query to execute | ||
| parameters | yes | Parameters of the query as list (if positional parameters are used in query) or as dictionary (if named parameters are used). Psycopg2 syntax is required for parameters. See: http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries |
# Fetch all tables in my_app database
- postgresql_query:
database: my_app
query: "SELECT * FROM pg_tables"
# Fetch table info for table "pg_statistic"
- postgresql_query:
database: my_app
query: "SELECT * FROM pg_tables WHERE tablename = %(table_name)s"
parameters:
table_name: pg_statistic
register: query_results| name | description | returned | type |
|---|---|---|---|
| executed_query | the body of the last query sent to the backend (including bound arguments) as bytes string | success | string |
| rows | list of rows. Each row is a dict indexed using the column name | success | list |
| rowCount | number of rows returned by the query | success | int |
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 C(postgresql), C(libpq-dev), and C(python-psycopg2) packages on the remote host before using this module.