Search code examples
postgresqlsalt-project

How to install PostgreSQL 13 with Saltstacked state?


The title is basically the question. I am using CentOS 8 and want to start using Saltstack states to install stuff like Postgresql-13, AdoptOpenJDK,...

    install-postgresql13-repository:
  cmd.run:
    - name: rpm -U --force https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

install-postgresql13-server:
  cmd.run:
    - name: dnf install postgresql13-server -y

postgresql-first-run-init:
  cmd.run:
    - name: /usr/pgsql-13/bin/postgresql-13-setup initdb
    - unless: stat /var/lib/pgsql/13/data/postgresql.conf
    - runas: root

start-postgresql13-server:
  cmd.run:
    - name: systemctl start postgresql-13

enable-postgresql13-autostart:
  cmd.run:
    - name: systemctl enable postgresql-13

but cmd.run is not the best option as far as i know.


Solution

  • The main benefit of using tool like Saltstack is the idempotence it offers by the way of "states".

    Without getting into the complete solution, I would recommend you to go through Salt states.

    For example the below code will run fine first time, but on second run it may give error. Even if it works, it is unnecessary.

      cmd.run:
        - name: rpm -U --force https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    

    Whereas if you use a Salt state modules like below, they will be updated only when required. Subsequent runs will not attempt to make a change.

    install-postgresql13-repository:
      pkg.installed:
        - sources:
            - pgdg-redhat-repo: https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    
    install-postgresql-server:
      pkg.installed:
        - name: postgresql13-server
    
    # and similarly use salt states for other tasks where applicable