Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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'})
SCHEMA_NAMES.each do |schema|
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{schema}")

This comment has been minimized.

Copy link

@thiagoramos23 thiagoramos23 commented Feb 19, 2015

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
You can’t perform that action at this time.