Skip to content

Instantly share code, notes, and snippets.

@leklund
Created December 13, 2012 21:34
Show Gist options
  • Save leklund/4280172 to your computer and use it in GitHub Desktop.
Save leklund/4280172 to your computer and use it in GitHub Desktop.
How to create a schema other than public when running rake db:setup. This will allow you to include a schema_searth_path in database.yml without causing rake db:setup to fail.
SCHEMA_NAMES = %w(foo bar)
namespace :db do
task :create do
config = Rails.configuration.database_configuration[Rails.env].merge!({'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(config)
SCHEMA_NAMES.each do |schema|
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{schema}")
end
end
end
@thiagoramos23
Copy link

Great example. I'll use that snippet. Thanks.
Maybe you should set the Authorization too. Ex:
"CREATE SCHEMA "#{schema_name}" AUTHORIZATION "#{pg_username}""

Just in case you need the owner to be another user other than postgres

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