ansible.builtin.postgresql_table (v2.9.24) — module

Create, drop, or modify a PostgreSQL table

| "added in version" 2.8 of ansible.builtin"

Authors: Andrei Klychkov (@Andersson007)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.24

Description

Allows to create, drop, rename, truncate a table, or change some table attributes.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create tbl2 in the acme database with the DDL like tbl1 with testuser as an owner
  postgresql_table:
    db: acme
    name: tbl2
    like: tbl1
    owner: testuser
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create tbl2 in the acme database and tablespace ssd with the DDL like tbl1 including comments and indexes
  postgresql_table:
    db: acme
    table: tbl2
    like: tbl1
    including: comments, indexes
    tablespace: ssd
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create test_table with several columns in ssd tablespace with fillfactor=10 and autovacuum_analyze_threshold=1
  postgresql_table:
    name: test_table
    columns:
    - id bigserial primary key
    - num bigint
    - stories text
    tablespace: ssd
    storage_params:
    - fillfactor=10
    - autovacuum_analyze_threshold=1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create an unlogged table in schema acme
  postgresql_table:
    name: acme.useless_data
    columns: waste_id int
    unlogged: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Rename table foo to bar
  postgresql_table:
    table: foo
    rename: bar
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Rename table foo from schema acme to bar
  postgresql_table:
    name: acme.foo
    rename: bar
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Set owner to someuser
  postgresql_table:
    name: foo
    owner: someuser
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Change tablespace of foo table to new_tablespace and set owner to new_user
  postgresql_table:
    name: foo
    tablespace: new_tablespace
    owner: new_user
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Truncate table foo
  postgresql_table:
    name: foo
    truncate: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Drop table foo from schema acme
  postgresql_table:
    name: acme.foo
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Drop table bar cascade
  postgresql_table:
    name: bar
    state: absent
    cascade: yes

Inputs

    
db:
    aliases:
    - login_db
    description:
    - Name of database to connect and where the table will be created.
    type: str

like:
    description:
    - Create a table like another table (with similar DDL). Mutually exclusive with I(columns),
      I(rename), and I(truncate).
    type: str

port:
    aliases:
    - login_port
    default: 5432
    description:
    - Database port to connect to.
    type: int

owner:
    description:
    - Set a table owner.
    type: str

state:
    choices:
    - absent
    - present
    default: present
    description:
    - The table state. I(state=absent) is mutually exclusive with I(tablespace), I(owner),
      I(unlogged), I(like), I(including), I(columns), I(truncate), I(storage_params) and,
      I(rename).
    type: str

table:
    aliases:
    - name
    description:
    - Table name.
    required: true
    type: str

rename:
    description:
    - New table name. Mutually exclusive with I(tablespace), I(owner), I(unlogged), I(like),
      I(including), I(columns), I(truncate), and I(storage_params).
    type: str

ca_cert:
    aliases:
    - ssl_rootcert
    description:
    - 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.
    type: str

cascade:
    default: false
    description:
    - Automatically drop objects that depend on the table (such as views). Used with I(state=absent)
      only.
    type: bool
    version_added: '2.9'
    version_added_collection: ansible.builtin

columns:
    description:
    - Columns that are needed.
    elements: str
    type: list

ssl_mode:
    choices:
    - allow
    - disable
    - prefer
    - require
    - verify-ca
    - verify-full
    default: prefer
    description:
    - Determines whether or with what priority a secure SSL TCP/IP connection will be
      negotiated with the server.
    - See U(https://www.postgresql.org/docs/current/static/libpq-ssl.html) for more information
      on the modes.
    - Default of C(prefer) matches libpq default.
    type: str

truncate:
    default: false
    description:
    - Truncate a table. Mutually exclusive with I(tablespace), I(owner), I(unlogged),
      I(like), I(including), I(columns), I(rename), and I(storage_params).
    type: bool

unlogged:
    default: false
    description:
    - Create an unlogged table.
    type: bool

including:
    description:
    - Keywords that are used with like parameter, may be DEFAULTS, CONSTRAINTS, INDEXES,
      STORAGE, COMMENTS or ALL. Needs I(like) specified. Mutually exclusive with I(columns),
      I(rename), and I(truncate).
    type: str

login_host:
    default: ''
    description:
    - Host running the database.
    - If you have connection issues when using C(localhost), try to use C(127.0.0.1) instead.
    type: str

login_user:
    default: postgres
    description:
    - The username this module should use to establish its PostgreSQL session.
    type: str

tablespace:
    description:
    - Set a tablespace for the table.
    required: false
    type: str

session_role:
    description:
    - 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.
    type: str

login_password:
    default: ''
    description:
    - The password this module should use to establish its PostgreSQL session.
    type: str

storage_params:
    description:
    - Storage parameters like fillfactor, autovacuum_vacuum_treshold, etc. Mutually exclusive
      with I(rename) and I(truncate).
    elements: str
    type: list

login_unix_socket:
    default: ''
    description:
    - Path to a Unix domain socket for local connections.
    type: str

Outputs

owner:
  description: Table owner.
  returned: always
  sample: postgres
  type: str
queries:
  description: List of executed queries.
  returned: always
  sample:
  - CREATE TABLE "test_table" (id bigint)
  type: str
state:
  description: Table state.
  returned: always
  sample: present
  type: str
storage_params:
  description: Storage parameters.
  returned: always
  sample:
  - fillfactor=100
  - autovacuum_analyze_threshold=1
  type: list
table:
  description: Name of a table.
  returned: always
  sample: foo
  type: str
tablespace:
  description: Tablespace.
  returned: always
  sample: ssd_tablespace
  type: str

See also