community.general.postgresql_table – Create, drop, or modify a PostgreSQL table
Note
This plugin is part of the community.general collection (version 1.3.2).
To install it use: ansible-galaxy collection install community.general.
To use it in a playbook, specify: community.general.postgresql_table.
Synopsis
- Allows to create, drop, rename, truncate a table, or change some table attributes.
 
Requirements
The below requirements are needed on the host that executes this module.
- psycopg2
 
Parameters
| Parameter | Choices/Defaults | Comments | 
|---|---|---|
|   ca_cert    string    |    Specifies the name of a file containing SSL certificate authority (CA) certificate(s).  If the file exists, the server's certificate will be verified to be signed by one of these authorities.  aliases: ssl_rootcert  |  |
|   cascade    boolean    |   
  |    Automatically drop objects that depend on the table (such as views). Used with state=absent only.   |  
|   columns    list / elements=string    |    Columns that are needed.   |  |
|   db    string    |    Name of database to connect and where the table will be created.  aliases: login_db  |  |
|   including    string    |    Keywords that are used with like parameter, may be DEFAULTS, CONSTRAINTS, INDEXES, STORAGE, COMMENTS or ALL. Needs like specified. Mutually exclusive with columns, rename, and truncate.   |  |
|   like    string    |    Create a table like another table (with similar DDL). Mutually exclusive with columns, rename, and truncate.   |  |
|   login_host    string    |    Host running the database.   |  |
|   login_password    string    |    The password used to authenticate with.   |  |
|   login_unix_socket    string    |    Path to a Unix domain socket for local connections.   |  |
|   login_user    string    |   Default: "postgres"   |    The username used to authenticate with.   |  
|   owner    string    |    Set a table owner.   |  |
|   port    integer    |   Default: 5432   |    Database port to connect to.  aliases: login_port  |  
|   rename    string    |    New table name. Mutually exclusive with tablespace, owner, unlogged, like, including, columns, truncate, and storage_params.   |  |
|   session_role    string    |    Switch to session_role after connecting. The specified session_role must be a role that the current login_user is a member of.  Permissions checking for SQL commands is carried out as though the session_role were the one that had logged in originally.   |  |
|   ssl_mode    string    |   
  |    Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.  See https://www.postgresql.org/docs/current/static/libpq-ssl.html for more information on the modes.  Default of   prefer matches libpq default. |  
|   state    string    |   
  |    The table state. state=absent is mutually exclusive with tablespace, owner, unlogged, like, including, columns, truncate, storage_params and, rename.   |  
|   storage_params    list / elements=string    |    Storage parameters like fillfactor, autovacuum_vacuum_treshold, etc. Mutually exclusive with rename and truncate.   |  |
|   table    string / required    |    Table name.  aliases: name  |  |
|   tablespace    string    |    Set a tablespace for the table.   |  |
|   truncate    boolean    |   
  |    Truncate a table. Mutually exclusive with tablespace, owner, unlogged, like, including, columns, rename, and storage_params.   |  
|   trust_input    boolean    added in 0.2.0 of community.general    |   
  |    If   no, check whether values of parameters are potentially dangerous.It makes sense to use   no only when SQL injections are possible. |  
|   unlogged    boolean    |   
  |    Create an unlogged table.   |  
Notes
Note
- If you do not pass db parameter, tables will be created in the database named postgres.
 - PostgreSQL allows to create columnless table, so columns param is optional.
 - Unlogged tables are available from PostgreSQL server version 9.1.
 - The default authentication assumes that you are either logging in as or sudo’ing to the 
postgresaccount on the host. - To avoid “Peer authentication failed for user postgres” error, use postgres user as a become_user.
 - 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.
 - The ca_cert parameter requires at least Postgres version 8.4 and psycopg2 version 2.4.3.
 
See Also
See also
- community.general.postgresql_sequence
 - 
The official documentation on the community.general.postgresql_sequence module.
 - community.general.postgresql_idx
 - 
The official documentation on the community.general.postgresql_idx module.
 - community.general.postgresql_info
 - 
The official documentation on the community.general.postgresql_info module.
 - community.general.postgresql_tablespace
 - 
The official documentation on the community.general.postgresql_tablespace module.
 - community.general.postgresql_owner
 - 
The official documentation on the community.general.postgresql_owner module.
 - community.general.postgresql_privs
 - 
The official documentation on the community.general.postgresql_privs module.
 - community.general.postgresql_copy
 - 
The official documentation on the community.general.postgresql_copy module.
 - CREATE TABLE reference
 - 
Complete reference of the CREATE TABLE command documentation.
 - ALTER TABLE reference
 - 
Complete reference of the ALTER TABLE command documentation.
 - DROP TABLE reference
 - 
Complete reference of the DROP TABLE command documentation.
 - PostgreSQL data types
 - 
Complete reference of the PostgreSQL data types documentation.
 
Examples
- name: Create tbl2 in the acme database with the DDL like tbl1 with testuser as an owner
  community.general.postgresql_table:
    db: acme
    name: tbl2
    like: tbl1
    owner: testuser
- name: Create tbl2 in the acme database and tablespace ssd with the DDL like tbl1 including comments and indexes
  community.general.postgresql_table:
    db: acme
    table: tbl2
    like: tbl1
    including: comments, indexes
    tablespace: ssd
- name: Create test_table with several columns in ssd tablespace with fillfactor=10 and autovacuum_analyze_threshold=1
  community.general.postgresql_table:
    name: test_table
    columns:
    - id bigserial primary key
    - num bigint
    - stories text
    tablespace: ssd
    storage_params:
    - fillfactor=10
    - autovacuum_analyze_threshold=1
- name: Create an unlogged table in schema acme
  community.general.postgresql_table:
    name: acme.useless_data
    columns: waste_id int
    unlogged: true
- name: Rename table foo to bar
  community.general.postgresql_table:
    table: foo
    rename: bar
- name: Rename table foo from schema acme to bar
  community.general.postgresql_table:
    name: acme.foo
    rename: bar
- name: Set owner to someuser
  community.general.postgresql_table:
    name: foo
    owner: someuser
- name: Change tablespace of foo table to new_tablespace and set owner to new_user
  community.general.postgresql_table:
    name: foo
    tablespace: new_tablespace
    owner: new_user
- name: Truncate table foo
  community.general.postgresql_table:
    name: foo
    truncate: yes
- name: Drop table foo from schema acme
  community.general.postgresql_table:
    name: acme.foo
    state: absent
- name: Drop table bar cascade
  community.general.postgresql_table:
    name: bar
    state: absent
    cascade: yes
   Return Values
Common return values are documented here, the following are the fields unique to this module:
| Key | Returned | Description | 
|---|---|---|
|   owner    string    |  always |   Table owner.  Sample:  postgres   |  
|   queries    string    |  always |   List of executed queries.  Sample:  ['CREATE TABLE "test_table" (id bigint)']   |  
|   state    string    |  always |   Table state.  Sample:  present   |  
|   storage_params    list / elements=string    |  always |   Storage parameters.  Sample:  ['fillfactor=100', 'autovacuum_analyze_threshold=1']   |  
|   table    string    |  always |   Name of a table.  Sample:  foo   |  
|   tablespace    string    |  always |   Tablespace.  Sample:  ssd_tablespace   |  
Authors
- Andrei Klychkov (@Andersson007)
 
    © 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
    https://docs.ansible.com/ansible/2.10/collections/community/general/postgresql_table_module.html