Skip to content

Instantly share code, notes, and snippets.

@liangzan
Last active December 15, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangzan/5308177 to your computer and use it in GitHub Desktop.
Save liangzan/5308177 to your computer and use it in GitHub Desktop.
pg_users_chef_recipe.rb
include_recipe "postgresql::server"
node[:notifymode][:db_users].each do |user|
execute "create-database-user" do
exists = <<-EOH
sudo -u postgres psql -U postgres -c "select * from pg_user where usename='#{user[:name]}'" | grep -c #{user[:name]}
EOH
command "echo \"CREATE ROLE #{user[:name]} with CREATEDB LOGIN PASSWORD '#{user[:password]}'\" | sudo -u postgres psql -U postgres"
not_if exists
end
end
node[:notifymode][:dbs].each do |db|
execute "create-database" do
exists = <<-EOH
sudo -u postgres psql -U postgres -c "select * from pg_database WHERE datname='#{db[:name]}'" | grep -c #{db[:name]}
EOH
command "sudo -u postgres createdb -U postgres -O #{db[:owner]} -E utf8 #{db[:name]}"
not_if exists
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment