Skip to content

Instantly share code, notes, and snippets.

@royratcliffe
Created November 19, 2014 14:59
Show Gist options
  • Save royratcliffe/1c3b327514ce82e8353b to your computer and use it in GitHub Desktop.
Save royratcliffe/1c3b327514ce82e8353b to your computer and use it in GitHub Desktop.
Creates PostgreSQL database roles.
# The production database uses a non-default user name for PostgreSQL
# connections. The user name corresponds to the app name. Use this Rake task to
# create the corresponding PostgreSQL role.
namespace :pg do
namespace :role do
task :create => :environment do
ActiveRecord::Base.configurations.select { |env, config|
config['adapter'] == 'postgresql'
}.collect { |env, config|
config['username']
}.compact.uniq.each do |username|
IO.popen('psql -d postgres', 'r+') do |io|
io.puts "create role #{username} login createdb;"
io.close_write
puts io.readlines
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment