title: Creating a multi-tenant application using PostgreSQL's schemas and Rails
body: <h2>Stuff I've already figured out</h2>
  <p>I'm learning how to create a multi-tenant application in Rails that serves data from different schemas based on what domain or subdomain is used to view the application.</p>
  <p>I already have a few concerns answered:</p>
  <ol>
  <li>How can you get subdomain-fu to work with domains as well? <a href="https://stackoverflow.com/questions/2308903/support-for-multiple-domains-subdomains-in-rails">Here's someone that asked the same question</a> which leads you to <a href="http://www.arctickiwi.com/blog/7-host-and-domain-based-routing-in-ruby-on-rails" rel="noreferrer">this blog</a>.</li>
  <li><strong>What database, and how will it be structured?</strong> Here's an excellent <a href="http://www.bestechvideos.com/2009/03/26/acts_as_conference-2009-writing-multi-tenant-applications-in-rails" rel="noreferrer">talk by Guy Naor</a>, and good <a href="https://stackoverflow.com/questions/1152405/postgresql-is-better-using-multiple-databases-with-1-schema-each-or-1-database">question about PostgreSQL and schemas</a>.</li>
  <li>I already know my schemas will all have the same structure. They will differ in the data they hold. So, <strong>how can you run migrations for all schemas?</strong> Here's <a href="https://stackoverflow.com/questions/1970564/rails-migrations-for-postgresql-schemas">an answer</a>.</li>
  </ol>
  <p>Those three points cover a lot of the general stuff I need to know. However, in the next steps I seem to have many ways of implementing things. I'm hoping that there's a better, easier way.</p>
  <h2>Finally, to my question</h2>
  <p>When a new user signs up, I can easily create the schema. However, <strong>what would be the best and easiest way to load the structure that the rest of the schemas already have?</strong> Here are some questions/scenarios that might give you a better idea.</p>
  <ol>
  <li>Should I pass it on to a <strong>shell script</strong> that dumps the public schema into a temporary one, and imports it back to my main database (pretty much like what Guy Naor says in his video)? Here's a <a href="http://pastebin.com/7C93iuH3" rel="noreferrer">quick summary/script I got from the helpful #postgres on freenode</a>. While this will probably work, I'm gonna have to do a lot of stuff outside of Rails, which makes me a bit uncomfortable.. which also brings me to the next question.</li>
  <li><strong>Is there a way to do this straight from Ruby on Rails</strong>? Like create a PostgreSQL schema, then just load the Rails database schema (schema.rb - I know, it's confusing) into that PostgreSQL schema.</li>
  <li><strong>Is there a gem/plugin that has these things already?</strong> Methods like &quot;create_pg_schema_and_load_rails_schema(the_new_schema_name)&quot;. If there's none, I'll probably work at making one, but I'm doubtful about how well tested it'll be with all the moving parts (especially if I end up using a shell script to create and manage new PostgreSQL schemas).</li>
  </ol>
  <p>Thanks, and I hope that wasn't too long!</p>