<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aldosoft &#187; Ruby on Rails</title>
	<atom:link href="http://aldosoft.com/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://aldosoft.com</link>
	<description>We build crisp, intuitive web-based systems</description>
	<lastBuildDate>Mon, 04 Apr 2011 05:16:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Installing PostgreSQL server and Ruby gem on Mac OS X</title>
		<link>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/</link>
		<comments>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 01:19:52 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[os-x]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.aldosoft.dev/?p=27</guid>
		<description><![CDATA[How to compile and install both the PostgreSQL relational database server and the associated Ruby database driver for Postgres, for doing Ruby on Rails development with PostgreSQL on Mac OS X 10.5 "Leopard".<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p>	<p>I recently needed to install the PostgreSQL database server on my Mac for development with Ruby on Rails. I ran into a few gotchas, and while I was eventually able to Google and guess my way to success, it took a while. This installation guide is as much for me, 6 months from now, as it is for others who may wish to also install Postgres.</p>

	<p>These instructions were current as of April 22, 2009, and were executed on Mac OS X 10.5.6 with Rubygems 1.3.2 and the Postgres 8.3.7 distribution. The shelf life of these instructions is probably brief, though they should work at least as a rough guide until the next major release of Mac OS X, or of Postgres.</p>

	<p><span id="more-27"></span></p>

	<h2>Preferences and Biases</h2>

	<p>There are other Postgres + Rails instructions which recommend the use of either MacPorts or Fink. I personally dislike these solutions; part of it is having used Fink in its early days and having it screw up royally, and part of it is a sense that the efforts are misplaced. (Why not make your Open Source contribution of how to get a project properly compiled and installed on Mac OS X directly in the project itself?) In any event, I refuse to use either&#8230;though I agree that they have a place for people less comfortable compiling software directly from source code.</p>

	<p>I also prefer not to use pre-compiled binary installations of Open Source tools, unless the maintainer has demonstrated a long-term commitment and expertise in building and maintaining the package. Marc Liyanage did a terrific job with <a href="http://www.entropy.ch/software/macosx/php/"><span class="caps">PHP</span> 5 for Mac OS X</a> releases up through &#8220;Tiger.&#8221; MySQL is doing a great job creating <a href="http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg">Mac OS X installer packages for the MySQL database</a>. But many binary installations either make odd decisions about where to put things, or don&#8217;t get maintained, or make module/configuration/packaging decisions different from what I need, or all three.</p>

	<p>So I generally prefer to compile from source code, and install my additions to Mac OS X in <code>/usr/local</code>. Dan Benjamin explains <a href="http://hivelogic.com/articles/view/using_usr_local/">the logic and benefits of <code>/usr/local</code></a> in exhausting detail, I won&#8217;t go into it further. Either you agree, and these instructions may be useful to you, or you don&#8217;t, and you should keep Googling.</p>

	<h2>Prerequisites and Preconditions</h2>

	<ul>
		<li>These instructions are for Mac OS X 10.5 &#8220;Leopard&#8221; only.</li>
		<li>You need to be comfortable, or better yet confident, on the command line.</li>
		<li>You need to have Apple&#8217;s developer tools installed.</li>
		<li>You need to have current versions of Ruby on Rails and Rubygems installed.</li>
		<li>You need to understand what it means to update your $PATH environment, preferrably in <code>~/.bash_login</code>. You <em>do</em> have a <code>~/.bash_login</code> file, right?</li>
	</ul>

	<p>But mostly this document assumes that you&#8217;ve already gone through the <a href="http://hivelogic.com/articles/view/ruby-rails-leopard/">outstanding instructions for installing a complete Ruby on Rails development suite</a> provided at Hivelogic. Indeed, if you didn&#8217;t use those instructions, at least mostly, you probably shouldn&#8217;t use mine either.</p>

	<h2>The Instructions</h2>

	<p>There are two things that need to be installed, the Postgres server and the Ruby gem for the Postgres database driver. You will also want to add a launchd task for starting and stopping the database server.</p>

	<h3>Installing the PostgreSQL Database Server</h3>

	<p>Postgres traditionally runs as a &#8220;daemon,&#8221; or background process, and manages all control to its files, especially its data. This requires that you create a dedicated user account and group, which only Postgres will use. While you <em>could</em> do this in the Accounts preference pane, it&#8217;s better to do this from the command line, so that the system won&#8217;t treat the Postgres user like a normal user. This is done with the Directory Service command line utility, <code>dscl</code>. </p>

	<p>Before creating the Postgres user you need to pick out internal ID numbers for the user and group. You need to find a number that is not currently in use, so start by seeing what <em>is</em> in use. View the list of user accounts and ID numbers (note that some output is elided):</p>

<pre class="terminal">$ <strong>dscl . -list /Users UniqueID</strong>
_amavisd           83
_appowner          87
_appserver         79
[...]
_mysql             74
[...]
alderete           501
daemon             1
nobody             -2
root               0</pre>

	<p>Next the groups:</p>

<pre class="terminal">$ <strong>dscl . -list /Groups PrimaryGroupID</strong>
_amavisd                       83
_appowner                      87
_appserveradm                  81
[...]
_mysql                         74
[...]
tty                            4
utmp                           45
wheel                          0</pre>

	<p>Within these lists you want to find an ID number under 500 that is not in use. My <cite href="http://www.working-software.com/node/30" title="Building and Installing PostgreSQL on Mac OS X">primary source for these instructions</cite> recommending adding 100 to the ID for MySQL, which seems reasonable. It&#8217;s also probably helpful to find a number that is available for both the user and the group ID, so you don&#8217;t have to worry about which is which. From the output above, I chose 174, which met both criteria. </p>

	<p>With chosen user and group IDs, create the user. Here we are creating a user with a $HOME of <code>/usr/local/pgsql</code>, the location where we will be installing Postgres. We&#8217;re also creating the user in such a way that it&#8217;s useful only as a daemon; it is not possible to log in to the system as this user, either at the normal login screen or via ssh.</p>

<pre class="terminal">$ <strong>sudo dscl . -create /Users/postgres UniqueID 174</strong>
$ <strong>sudo dscl . -create /Users/postgres PrimaryGroupID 174</strong>
$ <strong>sudo dscl . -create /Users/postgres HomeDirectory /usr/local/pgsql</strong>
$ <strong>sudo dscl . -create /Users/postgres UserShell /usr/bin/false</strong>
$ <strong>sudo dscl . -create /Users/postgres RealName "PostgreSQL Administrator"</strong>
$ <strong>sudo dscl . -create /Users/postgres Password \*</strong></pre>

	<p>Now verify that the user was created as expected, by reading back the values from the system:</p>

<pre class="terminal">$ <strong>dscl . -read /Users/postgres</strong>
AppleMetaNodeLocation: /Local/Default
GeneratedUID: 0024E028-00A9-48FB-B14A-68026D442870
HomeDirectory: /usr/local/pgsql
Password: *
PrimaryGroupID: 174
RealName:
 PostgreSQL Administrator
RecordName: postgres
RecordType: dsRecTypeStandard:Users
UniqueID: 174
UserShell: /usr/bin/false</pre>

	<p>Follow a similar procedure to create and verify the group:</p>

<pre class="terminal">$ <strong>sudo dscl . -create /Groups/postgres PrimaryGroupID 174</strong>
$ <strong>sudo dscl . -create /Groups/postgres Password \*</strong>
$ <strong>dscl . -read /Groups/postgresAppleMetaNodeLocation: /Local/Default</strong>
GeneratedUID: 7A7F8760-455F-4315-84DD-2DA330AD9845
Password: *
PrimaryGroupID: 174
RecordName: postgres
RecordType: dsRecTypeStandard:Groups</pre>

	<p>With the dedicated user and group created, you can now download, compile, and install Postgres. The <a href="http://www.postgresql.org/download/">source code distribution</a> can be downloaded from one of the Posgresql.org mirrors. If you use Safari to download it, be sure you&#8217;ve turned off the <span class="ui">Open &#8220;safe&#8221; files after downloading</span> option. (And leave it off, it is a <a href="http://daringfireball.net/linked/2006/02/21/safari-shell-scripts">known security risk</a>.)</p>

	<p>Move the archive to your preferred location (such as <code>/usr/local/src</code>), start a Terminal session there, and:</p>

<pre class="terminal">$ <strong>tar zxvf postgresql-8.3.7.tar.gz</strong>
$ <strong>cd postgresql-8.3.7</strong>
$ <strong>./configure</strong>
[...]
$ <strong>make</strong>
[...]
$ <strong>sudo make install</strong>
[...]</pre>

	<p><strong>Note:</strong> there may be options you want to enable with the configure script, such as encryption or authentication methods. Use <code>./configure --help</code> to get more details.</p>

	<p>Installing the software does not actually create a location to store the database data, or initialize the system databases. Do that next:</p>

<pre class="terminal">$ <strong>sudo mkdir /usr/local/pgsql/data</strong>
$ <strong>sudo chown postgres:postgres /usr/local/pgsql/data</strong>
$ <strong>sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data</strong>
Password:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
[...]
Success. You can now start the database server using:
    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start</pre>

	<p>You&#8217;ll want to create a Postgres user (distinct from the system user for Postgres), but before you can do that, you need to start the server. Below I describe how you can have Postgres start automatically when your computer boots, but for now, let&#8217;s just start it up manually:</p>

<pre class="terminal">$ <strong>/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start</strong></pre>

	<p>Now create that Postgres user, and a default database for it to use. This is the account that you will eventually use in the database.yml file of your Rails applications. And finally, use the <code>psql</code> command line tool to verify that we&#8217;ve successfully created the user and database.</p>

<pre class="terminal">$ <strong>sudo -u postgres createuser alderete</strong>
$ <strong>createdb</strong>
$ <strong>psql</strong>
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
[...]
alderete=# <strong>\l</strong>
               List of databases
           Name           |  Owner   | Encoding 
--------------------------+----------+----------
 alderete                 | alderete | UTF8
 postgres                 | postgres | UTF8
 template0                | postgres | UTF8
 template1                | postgres | UTF8
(4 rows)
alderete=# <strong>\q</strong></pre>

	<p><strong>Note:</strong> The above illustrates creating a database user and new database, both named for my Unix account name, &#8220;alderete&#8221;. Using the Unix account name simplifies using Postgres&#8217;s command line tools, you won&#8217;t have to enter the Postgres account name every time. But that will only work if your name is &#8220;alderete&#8221; too. ;-) Instead, use <em>your</em> Unix (or &#8220;short&#8221;) account name instead! This will likely be visible in your command line prompt, but it will also appear in the list of users you generated at the beginning of these instructions.</p>

	<p>Finally, add important Postgres directories to your various system paths. Add the following lines to your <code>~/.bash_login</code> file:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$PATH</span>:/usr/local/pgsql/bin&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MANPATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$MANPATH</span>:/usr/local/pgsql/man&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_LIBRARY_PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$LD_LIBRARY_PATH</span>:/usr/local/pgsql/lib&quot;</span></pre></div></div>


	<p>Be sure to source this file to activate the new environment settings:</p>

<pre class="terminal">$ <strong>. ~/.bash_login</strong></pre>

	<h3>Install the Postgres Ruby Gem</h3>

	<p>Normally installing a Ruby gem is pretty simple, but the Postgres database driver has native (compiled) components, and these need to be linked against the Postgres binary. The problem is that by default Postgres is only built for the hardware architecture of your computer (PowerPC <em>or</em> Intel), while the gem appears to try to link against <em>both</em> architectures. This causes an error, and the helpful suggestion that appears with the error message does not in fact work, producing the exact same error message.</p>

	<p>The trick to getting past this is to set the preferred architecture right on the gem install command, like so:</p>

<pre class="terminal">$ <strong>sudo env ARCHFLAGS='-arch i386' gem install \
  postgres -- --with-pgsql-dir=/usr/local/pgsql</strong>
Password:
Building native extensions.  This could take a while...
Successfully installed postgres-0.7.9.2008.01.28
1 gem installed</pre>

	<p>(If you&#8217;re still using a PowerPC-based computer, change &#8220;i386&#8221; to &#8220;ppc&#8221;.)</p>

	<p>This should allow the gem to install successfully.</p>

	<h3>Automatically Starting Postgres at System Startup</h3>

	<p>On my development systems I&#8217;m using the database constantly, and so I have it set to start up automatically, as part of my computer&#8217;s boot process. The &#8220;Leopard&#8221; way to do this is to add a launchd task to your system. This is a text file that you&#8217;ll save on your desktop, and then move to <code>/Library/LaunchDaemons/org.postgresql.dbms.plist</code>. The following was copied from my primary source (see below):</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Label<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.postgresql.dbms<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UserName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>postgres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GroupName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>postgres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ProgramArguments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/bin/postmaster<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-D<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>StandardErrorPath<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/logs/postgresql.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RunAtLoad<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


	<p>Once this file is in place, you can load it by restarting your computer, or with this command:</p>

<pre class="terminal">$ <strong>sudo launchctl load /Library/LaunchDaemons/org.postgresql.dbms.plist</strong></pre>

	<p>If you can believe it, you&#8217;re done. Pour a cold one, or get right to it with <code>rails mynewapp -d postgresql</code>. Good luck!</p>

	<h2>Sources, Citations, and Credits</h2>

	<p>By far the most complete posting on this topic was <br />
Working Software&#8217;s <a href="http://www.working-software.com/node/30">Building and Installing PostgreSQL on Mac OS X</a>, which covered installing Postgres from source completely. While the instructions above are copy/pasted from my Terminal windows, I was copying lines directly from this article to paste <em>into</em> Terminal in the first place.</p>

	<p>The one thing that wasn&#8217;t covered in the above article was installing the Ruby gem for Postgres, which is required to use Postgres in Rails. The normal <code>gem install [gemname]</code> produced errors, and most suggestions I found didn&#8217;t work any better. What finally did work was including the &#8220;-arch i382&#8221; option directly on the gem installation line, which appears to be orginally published towards the bottom of <a href="http://brionesandco.com/ryanbriones/2008/01/postgresql-on-leopard-for-rails-few.html">PostgreSQL on Leopard for Rails: A few notes</a>. (A few other posts repeat this advice, so I&#8217;m not sure who first published it.)</p>

	<p>Finally, although it&#8217;s quite dated, the Apple Developer Connection article <a href="http://developer.apple.com/internet/opensource/postgres.html">PostgreSQL on Mac OS X</a> provides useful background information, and notes about using Postgres from other languages besides Ruby.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p></p>]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

