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.


Join the 80/20 DevOps Newsletter

If you're an engineering leader or developer, you should subscribe to my 80/20 DevOps Newsletter. Give me 1 minute of your day, and I'll teach you essential DevOps skills. I cover topics like Kubernetes, AWS, Infrastructure as Code, and more.

Not sure yet? Check out the archive.

Unsubscribe at any time.