Skip to content

postgresql_command

Denis Gasparin edited this page Oct 8, 2017 · 2 revisions

postgresql_command: execute a command in a PostGreSQL database and return the affected rows number

Synopsis

  • execute a command in a PostGreSQL database and return the affected rows number

Requirements

These are requirements in the host that executes the module:

  • psycopg2

Options

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.
command yes The SQL command to execute
parameters yes Parameters of the SQL command 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

Examples

- postgresql_command:
    database: my_app
    command: "UPDATE my_table SET status = FALSE"

# Set the field status to FALSE for rows with id less than 10
- postgresql_command:
    database: my_app
    command: "UPDATE my_table SET status = FALSE AND id < %(id)s"
    parameters:
      id: 10
  register: command_results

Return Values

name description returned type
executed_command the body of the SQL command sent to the backend (including bound arguments) as bytes string success string
rowCount number of rows returned by the query success string

Notes

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.

Clone this wiki locally