community.postgresql.postgresql_table module – Create, drop, or modify a PostgreSQL table
Note
This module is part of the community.postgresql collection (version 3.7.0).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.postgresql
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: community.postgresql.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 >= 2.5.1
Parameters
Parameter |
Comments |
---|---|
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. |
|
Automatically drop objects that depend on the table (such as views). Used with state=absent only. Choices:
|
|
Columns that are needed. |
|
Any additional parameters to be passed to libpg. These parameters take precedence. Default: |
|
Name of database to connect and where the table will be created. Default: |
|
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. |
|
Create a table like another table (with similar DDL). Mutually exclusive with columns, rename, and truncate. |
|
Host running the database. If you have connection issues when using Default: |
|
The password this module should use to establish its PostgreSQL session. Default: |
|
Path to a Unix domain socket for local connections. Default: |
|
The username this module should use to establish its PostgreSQL session. Default: |
|
Set a table owner. |
|
Database port to connect to. Default: |
|
New table name. Mutually exclusive with tablespace, owner, unlogged, like, including, columns, truncate, and storage_params. |
|
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. |
|
Specifies the file name of the client SSL certificate. |
|
Specifies the location for the secret key used for the client certificate. |
|
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 Choices:
|
|
The table state. state=absent is mutually exclusive with tablespace, owner, unlogged, like, including, columns, truncate, storage_params and, rename. Choices:
|
|
Storage parameters like fillfactor, autovacuum_vacuum_treshold, etc. Mutually exclusive with rename and truncate. |
|
Table name. |
|
Set a tablespace for the table. |
|
Truncate a table. Mutually exclusive with tablespace, owner, unlogged, like, including, columns, rename, and storage_params. Choices:
|
|
If It makes sense to use Choices:
|
|
Create an unlogged table. Choices:
|
Attributes
Attribute |
Support |
Description |
---|---|---|
Support: full |
Can run in check_mode and return changed status prediction without modifying target. |
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
postgres
account on the host.To avoid “Peer authentication failed for user postgres” error, use postgres user as a become_user.
This module uses
psycopg
, a Python PostgreSQL database adapter. You must ensure thatpsycopg2 >= 2.5.1
orpsycopg3 >= 3.1.8
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
, andpython3-psycopg2
packages on the remote host before using this module.
See Also
See also
- community.postgresql.postgresql_sequence
Create, drop, or alter a PostgreSQL sequence.
- community.postgresql.postgresql_idx
Create or drop indexes from a PostgreSQL database.
- community.postgresql.postgresql_info
Gather information about PostgreSQL servers.
- community.postgresql.postgresql_tablespace
Add or remove PostgreSQL tablespaces from remote hosts.
- community.postgresql.postgresql_owner
Change an owner of PostgreSQL database object.
- community.postgresql.postgresql_privs
Grant or revoke privileges on PostgreSQL database objects.
- community.postgresql.postgresql_copy
Copy data between a file/program and a PostgreSQL table.
- 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.postgresql.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.postgresql.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.postgresql.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.postgresql.postgresql_table:
name: acme.useless_data
columns: waste_id int
unlogged: true
- name: Rename table foo to bar
community.postgresql.postgresql_table:
table: foo
rename: bar
- name: Rename table foo from schema acme to bar
community.postgresql.postgresql_table:
name: acme.foo
rename: bar
- name: Set owner to someuser
community.postgresql.postgresql_table:
name: foo
owner: someuser
- name: Change tablespace of foo table to new_tablespace and set owner to new_user
community.postgresql.postgresql_table:
name: foo
tablespace: new_tablespace
owner: new_user
- name: Truncate table foo
community.postgresql.postgresql_table:
name: foo
truncate: true
- name: Drop table foo from schema acme
community.postgresql.postgresql_table:
name: acme.foo
state: absent
- name: Drop table bar cascade
community.postgresql.postgresql_table:
name: bar
state: absent
cascade: true
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Table owner. Returned: success Sample: |
|
List of executed queries. Returned: success Sample: |
|
Table state. Returned: success Sample: |
|
Storage parameters. Returned: success Sample: |
|
Name of a table. Returned: success Sample: |
|
Tablespace. Returned: success Sample: |