Dumping/Restoring Postgres With Ansible

I was playing with Ansible again for a side project.

One cool thing I learned was that there’s a plugin for Postgresql that makes it easier to do common operations, such as dumping and restoring a database.

Here’s a snippet of what it looks like to do both operations:

- name: "Dump database to a file {{ db_name }}"
  community.postgresql.postgresql_db:
    name: "{{ db_name }}"
    state: dump
    target: "/tmp/{{ db_name }}.sql"
    login_host: "{{ db_host }}"
    login_user: "{{ db_username }}"
    login_password: "{{ db_password }}"
    port: "{{ db_port }}"

- name: "Restore database {{ db_name }}"
  community.postgresql.postgresql_db:
    name: "{{ db_name }}"
    state: restore
    target: "/tmp/{{ db_name }}.sql"
    login_host: "{{ db_host }}"
    login_user: "{{ db_username }}"
    login_password: "{{ db_password }}"
    port: "{{ db_port }}"

I plan on using a mixture of Ansible and Terraform to deploy my small side project to AWS. I want to have a write-up of that after I set it up.


Master GitHub Actions with a Senior Infrastructure Engineer

As a senior staff infrastructure engineer, I share exclusive, behind-the-scenes insights that you won't find anywhere else. Get the strategies and techniques I've used to save companies $500k in CI costs and transform teams with GitOps best practices—delivered straight to your inbox.

Not sure yet? Check out the archive.

Unsubscribe at any time.