Muthukumar
Muthukumar

Reputation: 9579

Modify schema name of postgresql db from "public" to "my-project"

I use ActiveRecord to migrate the changes to postgresql db. The schema name by default is "public". Where is the best way to rename this to "my-project-name".?

Upvotes: 1

Views: 1891

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

There is no such method in ActiveRecord::Migration to alter scheme name, but you able to use plain sql command.

First generate migration file:

$> bundle exec rails g migration RenameSchema

Then open it and add:

class RenameSchema < ActiveRecord::Migration
  def up
    execute "ALTER SCHEMA you_old_name RENAME TO you_new_name" 
  end

  def down
    execute "ALTER SCHEMA you_new_name RENAME TO you_old_name" 
  end
end

With that migration you can change name and rollback it back.

Note: You must own the schema to use ALTER SCHEMA. To rename a schema you must also have the CREATE privilege for the database.

Upvotes: 3

Related Questions