Skip to content

Instantly share code, notes, and snippets.

@coderanger
Created October 20, 2010 23:29
Show Gist options
  • Save coderanger/637579 to your computer and use it in GitHub Desktop.
Save coderanger/637579 to your computer and use it in GitHub Desktop.
Create postgres users and databases from Chef
#
# Cookbook Name:: atari
# Recipe:: db_server
#
# Copyright 2010, Atari
#
# All rights reserved
#
include_recipe "postgresql::server"
r = package "ruby-pg" do
package_name "libpgsql-ruby"
action :nothing
end
r.run_action(:upgrade)
# Snippet from opscode to reload gems
require 'rubygems'
Gem.clear_paths
require "pg"
execute "create-database-user" do
command "createuser -U postgres -SDRw youruser"
only_if do
c = PGconn.connect(:user=>'postgres', :dbname=>'postgres')
r = c.exec("SELECT COUNT(*) FROM pg_user WHERE usename='youruser'")
r.entries[0]['count']
end
end
execute "create-database" do
command "createdb -U postgres -O youruser-E utf8 -T template0 yourdb"
only_if do
c = PGconn.connect(:user=>'postgres', :dbname=>'postgres')
r = c.exec("SELECT COUNT(*) FROM pg_database WHERE datname='yourdb'")
r.entries[0]['count']
end
end
@honza
Copy link

honza commented Sep 12, 2011

Where should this code live?

@coderanger
Copy link
Author

This is just a recipe, but a better version of it is in progress in my COOK-634 branch which provides an LWRP structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment