postgresql_db – Add or remove PostgreSQL databases from a remote host¶
Parameters¶
Notes¶
Note
State
dump
andrestore
don’t require psycopg2 since version 2.8.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 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
- CREATE DATABASE reference
Complete reference of the CREATE DATABASE command documentation.
- DROP DATABASE reference
Complete reference of the DROP DATABASE command documentation.
- pg_dump reference
Complete reference of pg_dump documentation.
- pg_restore reference
Complete reference of pg_restore documentation.
- postgresql_tablespace – Add or remove PostgreSQL tablespaces from remote hosts
The official documentation on the postgresql_tablespace module.
- postgresql_info – Gather information about PostgreSQL servers
The official documentation on the postgresql_info module.
- postgresql_ping – Check remote PostgreSQL server availability
The official documentation on the postgresql_ping module.
Examples¶
- name: Create a new database with name "acme"
postgresql_db:
name: acme
# Note: If a template different from "template0" is specified, encoding and locale settings must match those of the template.
- name: Create a new database with name "acme" and specific encoding and locale # settings.
postgresql_db:
name: acme
encoding: UTF-8
lc_collate: de_DE.UTF-8
lc_ctype: de_DE.UTF-8
template: template0
# Note: Default limit for the number of concurrent connections to a specific database is "-1", which means "unlimited"
- name: Create a new database with name "acme" which has a limit of 100 concurrent connections
postgresql_db:
name: acme
conn_limit: "100"
- name: Dump an existing database to a file
postgresql_db:
name: acme
state: dump
target: /tmp/acme.sql
- name: Dump an existing database to a file (with compression)
postgresql_db:
name: acme
state: dump
target: /tmp/acme.sql.gz
- name: Dump a single schema for an existing database
postgresql_db:
name: acme
state: dump
target: /tmp/acme.sql
target_opts: "-n public"
# Note: In the example below, if database foo exists and has another tablespace
# the tablespace will be changed to foo. Access to the database will be locked
# until the copying of database files is finished.
- name: Create a new database called foo in tablespace bar
postgresql_db:
name: foo
tablespace: bar
Status¶
This module is guaranteed to have backward compatible interface changes going forward. [stableinterface]
This module is maintained by the Ansible Community. [community]
Authors¶
Ansible Core Team
Hint
If you notice any issues in this documentation, you can edit this document to improve it.