Search code examples
postgresqlpuppet

Using puppet to install postgres without puppet-postgres


I am trying to install postgresql, set up a database and a user, but I am stuck at the setting up a database and use part. What I have so far is:

# Install postgres
class postgres::install {

  package { [
    'postgresql',
    'postgresql-contrib',
  ]:
  ensure => "installed",
  }
}

But now how do I use this to create a user, database and grant all permissions on that database to that user?


Solution

  • You can do this a couple of ways, but the easiest way to do it repeatedly is with a define that calls a couple of execs:

    Exec {
      path => [
        '/usr/local/sbin',
        '/usr/local/bin',
        '/usr/bin',
        '/usr/sbin',
        '/bin',
        '/sbin',
      ]
    }
    define postgres::db_setup (
      $dbname,
    ){
      exec { "configure_db_${dbname}":
        command => "pg command here using ${dbname}; touch success.txt"
        creates => "db_${dbname}_success.txt"
      }
    }
    define postgres::user_setup (
      $dbuser,
      $dbpassword,
      $dbname,
    ){      
      exec { "configure_user_${dbuser}_on_${dbname}":
        command => "pg command here using ${dbuser} on ${dbname} identified by ${dbpassword}; touch usersuccess.txt"
        creates => "user_${dbuser}_success.txt"
      }
    

    Then when you call the defines:

    postgres::db_setup { 'this_new_db':
      dbname => 'mynewdbname'
    }
    
    postgres::db_user { 'this_new_db_user_on_dbname':
      dbuser     => 'mynewdbuser',
      dbpassword => 'blahblah',
      dbname     => 'mynewdbname',
      require    => Postgres::Db_setup['this_new_db'],
    }
    

    This is a very dirty way to accomplish what you want by using dummy files to register when an exec has been completed. In my MySQL environment I use execs on scripts created with erb templates to allow more refined error checking.