Q:

Using Puppet to install, initdb and start Postgres database

We are trying to write a Puppet to provision the Virtual Machine.  One of the tasks is to install and populate Postgres database with data.
We can decompose this task into a few smaller tasks:

install
initdb
start
populate database with data (pg_dump from production database)

It seems Puppet will be responsible for Step 1: install.
Should we use Puppet to perform Step 2, 3, 4 as well?

A:

A common pattern when using puppet is managing the installation, configuration and service status of a package. You last step can also be achieved as long as you ensure idempotency. A (very) simple skeleton for this kind of task:

class foo (
  $some_param_in_hiera,
  ){

  package {
    'foo_pkg':
      ensure => installed,
  }

  service {
    'foo_svc':
      ensure    => running,
      enable    => true,
      hasstatus => true,
      require   => Package['foo_pkg'];
  }

  file {
    '/etc/foo/some.conf':
      mode    => '0644',
      owner   => root,
      group   => root,
      source  => 'puppet:///modules/foo/some.conf',
      #content => template('foo/some.conf.erb'),
      require => Package['foo_pkg'],
      notify  => Service['foo_svc'];
  }

  exec {
    'some_task':
      command     => 'echo 'foo bar baz'',
      refreshonly => true,
      creates     => '/some/file/to/ensure/idempotency'
  }
}

Also, check the Puppet Forge for already existing code that does what you want.

