<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Jonathan's Pancheria: Tag schemas</title>
    <link>http://blog.dotbot.net/articles/tag/schemas?tag=schemas</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>dotcom Thousandaire</description>
    <item>
      <title>Listing schemas in a Postgresql database</title>
      <description>To list the schemas in a postgresql database:
&lt;blockquote&gt;select nspname from pg_namespace;&lt;/blockquote&gt;</description>
      <pubDate>Tue, 01 Nov 2005 05:20:17 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:835572a1689e0f5b1d93ce35cc1f8d9f</guid>
      <author>Jonathan Altman</author>
      <link>http://blog.dotbot.net/articles/2005/11/01/listing-schemas-in-a-postgresql-database</link>
      <category>technical</category>
      <category>postgresql</category>
      <category>sql</category>
      <category>schemas</category>
      <category>list</category>
      <trackback:ping>http://blog.dotbot.net/articles/trackback/2</trackback:ping>
    </item>
    <item>
      <title>Hosting multiple Rails apps in a single postgresql database via schemas</title>
      <description>&lt;p&gt;I wanted a nice convenient way to have multiple Rails apps share my single postgresql database but keep all the apps&amp;#8217; tables/procs/views/other artifacts isolated from each other.  I came up with the following solution, which I can&amp;#8217;t claim is necessarily original, but I did not find documentation that laid out the solution all in one place.  I currently have the typo blog you are reading this article on up using this solution.&lt;/p&gt;


	&lt;p&gt;The trick is to use multiple Postgresql schemas (part of Postgresql since 7.4) to separate each Rails app&amp;#8217;s tables etc. into their own schema space  Then, every time you want to load up a new Rails app, or really any postgresql-based application, you just create a new schema.  You could in theory even put your development, testing, and production databases for a single Rails app into separate schemas in one database.&lt;/p&gt;


Documentation on Postgresql schemas, and more information about what the recipe below is doing with schema commands is &lt;a href="http://www.postgresql.org/docs/8/static/ddl-schemas.html"&gt;here&lt;/a&gt;.  Rails has had support for leveraging Postgresql schemas since version 0.11.0 as mentioned &lt;a href="http://dev.rubyonrails.com/ticket/827"&gt;here&lt;/a&gt;.
	&lt;h3&gt;Steps to create a schema in a postgresql database and configure a Rails app to use the schema&lt;/h3&gt;


1.  Create the new schema in your database:
	&lt;blockquote&gt;
		&lt;p&gt;CREATE &lt;span class="caps"&gt;SCHEMA &lt;/span&gt;&lt;i&gt;myrailsapp&lt;/i&gt;\g&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Replace &lt;i&gt;myrailsapp&lt;/i&gt; with whatever name you want for your schema in the &lt;span class="caps"&gt;SQL&lt;/span&gt; above and everywhere else following.&lt;/p&gt;


2.  If you are installing an existing Rails app that has a &lt;span class="caps"&gt;SQL&lt;/span&gt; script to generate the tables etc., then insert the following as the first line of the schema file, otherwise skip to step 4:
	&lt;blockquote&gt;
		&lt;p&gt;SET search_path &lt;span class="caps"&gt;TO &lt;/span&gt;&lt;i&gt;myrailsapp&lt;/i&gt;;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Postgresql uses the first schema in the schema path as the default location to look for tables/procs/triggers/etc if the name is unspecified (see &lt;a href="url=http://www.postgresql.org/docs/8/static/ddl-schemas.html#DDL-SCHEMAS-PATH]this"&gt;this discussion&lt;/a&gt; discussion in the Postgresql docs for more details).&lt;/p&gt;


3.  Execute the &lt;span class="caps"&gt;SQL&lt;/span&gt; script with the change from step 2.  If you go into psql and issue the following commands, you should see the objects that your script created:
&lt;blockquote&gt;
&lt;span class="caps"&gt;SET&lt;/span&gt; search_path &lt;span class="caps"&gt;TO &lt;/span&gt;&lt;i&gt;myrailsapp&lt;/i&gt;\g
&lt;br/&gt;
\d
&lt;/blockquote&gt;

4.  Edit  your database.yml file and add the following to the appropriate section(s):
	&lt;blockquote&gt;
		&lt;p&gt;schema_search_path: &lt;i&gt;myrailsapp&lt;/i&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


So your database.yml section for production would look like this if you wanted to use the myrailsapp schema in the mydbname database:
&lt;blockquote&gt;
production:&lt;br/&gt;
  &amp;nbsp;&amp;nbsp;adapter: postgresql&lt;br/&gt;
  &amp;nbsp;&amp;nbsp;database: &lt;i&gt;mydbname&lt;/i&gt;&lt;br/&gt;
 &amp;nbsp;&amp;nbsp;#whatever other postgresql config options you require&lt;br/&gt;
  &amp;nbsp;&amp;nbsp;&lt;b&gt;schema_search_path: &lt;i&gt;myrailsapp&lt;/i&gt;&lt;/b&gt;&lt;/br&gt;
&lt;/blockquote&gt;

	&lt;p&gt;That schema_search_path: statement in your database.yml file tells Rails to set the schema search path to look only in the &lt;i&gt;myrailsapp&lt;/i&gt; schema for unqualified database object names.  That means that without changing anything about how you write your code, your Rails app now has all its artifacts in a schema inside your database.&lt;/p&gt;


	&lt;p&gt;Enjoy!&lt;/p&gt;


	&lt;p&gt;Jonathan&lt;/p&gt;</description>
      <pubDate>Tue, 01 Nov 2005 04:27:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9e089c96cfe45acc383539d6da468ecb</guid>
      <author>Jonathan Altman</author>
      <link>http://blog.dotbot.net/articles/2005/11/01/hosting-multiple-rails-apps-in-a-single-postgresql-database-via-schemas</link>
      <category>technical</category>
      <category>postgresql</category>
      <category>schemas</category>
      <category>rails</category>
      <category>multiple</category>
      <trackback:ping>http://blog.dotbot.net/articles/trackback/1</trackback:ping>
    </item>
  </channel>
</rss>
