title: Impossible to Install PG gem on my mac with Mavericks
body: <p>I'm trying to install the pg gem in order to work again with my rails projects. But I get this error:</p>
  
  <blockquote>
    <p>Building native extensions.  This could take a while... ERROR:  Error
    installing pg:    ERROR: Failed to build gem native extension.</p>
  
  <pre><code>/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb checking for pg_config... no No pg_config... trying anyway. If
  </code></pre>
    
    <p>building fails, please try again with 
    --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header
    <strong>* extconf.rb failed *</strong> Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.  Check
    the mkmf.log file for more details.  You may need configuration
    options.</p>
    
    <p>Provided configuration options:   --with-opt-dir  --without-opt-dir
      --with-opt-include  --without-opt-include=${opt-dir}/include
      --with-opt-lib  --without-opt-lib=${opt-dir}/lib    --with-make-prog
      --without-make-prog     --srcdir=.  --curdir
      --ruby=/Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
      --with-pg   --without-pg    --with-pg-config    --without-pg-config
      --with-pg_config    --without-pg_config     --with-pg-dir
      --without-pg-dir    --with-pg-include
      --without-pg-include=${pg-dir}/include  --with-pg-lib
      --without-pg-lib=${pg-dir}/</p>
    
    <p>Gem files will remain installed in
    /Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0
    for inspection. Results logged to
    /Users/jeanosorio/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/pg-0.17.0/ext/gem_make.out</p>
  </blockquote>
  
  <p>I tried everything I found on stackoverflow but I still get this error.</p>
  
  <p>If I try to install postgresql using brew I get the following waring:</p>
  
  <blockquote>
    <p>Warning: postgresql-9.2.4 already installed, it's just not linked</p>
  </blockquote>
  
  <p>If I try to link</p>
  
  <blockquote>
    <p>brew link postgresql Linking /usr/local/Cellar/postgresql/9.2.4...
    Warning: Could not link postgresql. Unlinking...</p>
    
    <p>Error: Could not symlink file:
    /usr/local/Cellar/postgresql/9.2.4/share/man/man7/WITH.7
    /usr/local/share/man/man7 is not writable. You should change its
    permissions.</p>
  </blockquote>
  
  <p>Help Please</p>
  
  <p>NOTE: I already installed the command line tools for mavericks.</p>
  
  <hr>
  
  <p>If I uninstall using homebrew and try to install again, I get this error:</p>
  
  <blockquote>
    <p>==> Downloading <a href="http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2">http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2</a></p>
    
    ################################################################## 100.0%
    
    <p>==> Patching patching file src/pl/plpython/Makefile patching file contrib/uuid-ossp/uuid-ossp.c
    ==> ./configure --prefix=/usr/local/Cellar/postgresql/9.2.4 --datadir=/usr/local/Cellar/postgresql/9.2.4/share/postgresql --docdir=/usr/local/Cellar/p
    ==> make install-world
    ==> Caveats</p>
    
    <h1>Build Notes</h1>
    
    <p>If builds of PostgreSQL 9 are failing and you have version 8.x
    installed, you may need to remove the previous version first. See:<br>
    <a href="https://github.com/mxcl/homebrew/issues/issue/2510">https://github.com/mxcl/homebrew/issues/issue/2510</a></p>
    
    <h1>Create/Upgrade a Database</h1>
    
    <p>If this is your first install, create a database with:   initdb
    /usr/local/var/postgres -E utf8</p>
    
    <p>To migrate existing data from a previous major version (pre-9.2) of
    PostgreSQL, see:<br>
    <a href="http://www.postgresql.org/docs/9.2/static/upgrading.html">http://www.postgresql.org/docs/9.2/static/upgrading.html</a></p>
    
    <h1>Loading Extensions</h1>
    
    <p>By default, Homebrew builds all available Contrib extensions. To see a
    list of all available extensions, from the psql command line, run:<br>
    SELECT * FROM pg_available_extensions;</p>
    
    <p>To load any of the extension names, navigate to the desired database
    and run:   CREATE EXTENSION [extension name];</p>
    
    <p>For instance, to load the tablefunc extension in the current database,
    run:   CREATE EXTENSION tablefunc;</p>
    
    <p>For more information on the CREATE EXTENSION command, see:<br>
    <a href="http://www.postgresql.org/docs/9.2/static/sql-createextension.html">http://www.postgresql.org/docs/9.2/static/sql-createextension.html</a> For
    more information on extensions, see:<br>
    <a href="http://www.postgresql.org/docs/9.2/static/contrib.html">http://www.postgresql.org/docs/9.2/static/contrib.html</a></p>
    
    <h1>Other</h1>
    
    <p>Some machines may require provisioning of shared memory:<br>
    <a href="http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC">http://www.postgresql.org/docs/9.2/static/kernel-resources.html#SYSVIPC</a>
    When installing the postgres gem, including ARCHFLAGS is recommended: 
    ARCHFLAGS="-arch x86_64" gem install pg</p>
    
    <p>To install gems without sudo, see the Homebrew wiki.</p>
    
    <p>To have launchd start postgresql at login:
        ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now:
        launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't
    want/need launchctl, you can just run:
        pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start Warning: Could not link
    postgresql. Unlinking... Error: The <code>brew link</code> step did not complete
    successfully The formula built, but is not symlinked into /usr/local
    You can try again using `brew link postgresql'
    ==> Summary   /usr/local/Cellar/postgresql/9.2.4: 2831 files, 38M, built in 4.9 minutes</p>
  </blockquote>
  
  <hr>
  
  <p>SOLUTION:</p>
  
  <p>I execute this command in order to change the permission of the folder:</p>
  
  <blockquote>
    <p>sudo chown jeanosorio /usr/local/share/man/man7</p>
  </blockquote>
  
  <p>Then </p>
  
  <blockquote>
    <p>brew link postgresql Linking /usr/local/Cellar/postgresql/9.3.1... 421
    symlinks created</p>
  </blockquote>
  
  <p>And finally:</p>
  
  <blockquote>
    <p>sudo ARCHFLAGS="-arch x86_64" gem install pg</p>
    
    <p>Fetching: pg-0.17.0.gem (100%) Building native extensions.  This could
    take a while... Successfully installed pg-0.17.0</p>
  </blockquote>