Q:

Error during installation of both PG and Bundle

I'm in the final lesson of my first RoR tutorial and I'm being directed to open up terminal and input    install bundle 
The result:
tonys-mbp:crumblr Tony-MBP$ bundle install
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.11
Using json 1.8.1
Using minitest 5.4.2
Using thread_safe 0.3.4
Using tzinfo 1.2.2
Using activesupport 4.1.6
Using builder 3.2.2
Using erubis 2.7.0
Using actionview 4.1.6
Using rack 1.5.2
Using rack-test 0.6.2
Using actionpack 4.1.6
Using mime-types 2.4.1
Using mail 2.6.1
Using actionmailer 4.1.6
Using activemodel 4.1.6
Using arel 5.0.1.20140414130214
Using activerecord 4.1.6
Using bundler 1.7.3
Using coffee-script-source 1.8.0
Using execjs 2.2.1
Using coffee-script 2.3.0
Using thor 0.19.1
Using railties 4.1.6
Using coffee-rails 4.0.1
Using hike 1.2.3
Using multi_json 1.10.1
Using jbuilder 2.2.2
Using jquery-rails 3.1.2
/Users/Tony-MBP/.rvm/gems/ruby-2.1.3/gems/bundler-1.7.3/lib/bundler.rb:302: warning: Insecure world writable dir /usr/local in PATH, mode 040777

Gem::Ext::BuildError: ERROR: Failed to build gem native extension. 

    /Users/Tony-MBP/.rvm/rubies/ruby-2.1.3/bin/ruby extconf.rb 
checking for pg_config... no
No pg_config... trying anyway.  If 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
*** extconf.rb failed ***
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. 

Provided configuration options:
    --with-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/Tony-MBP/.rvm/rubies/ruby-2.1.3/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}/lib

extconf failed, exit code 1

Gem files will remain installed in /Users/Tony-MBP/.rvm/gems/ruby-2.1.3/gems/pg-0.17.1 for inspection. 
Results logged to /Users/Tony-MBP/.rvm/gems/ruby-2.1.3/extensions/x86_64-darwin-13/2.1.0/pg-0.17.1/gem_make.out
An error occurred while installing pg (0.17.1), and Bundler cannot continue. 
Make sure that `gem install pg -v '0.17.1'` succeeds before bundling. 

So I tried installing pg so I can continue on with Bundler; however, I get this error when I input: gem install pg -v '0.17.1':
Result:
tonys-mbp:crumblr Tony-MBP$ gem install pg
Building native extensions.   This could take a while...
/Users/Tony-MBP/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/ext/builder.rb:73: warning: Insecure world writable dir /usr/local in PATH, mode 040777
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension. 

    /Users/Tony-MBP/.rvm/rubies/ruby-2.1.3/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway.  If 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
*** extconf.rb failed ***
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. 

Provided configuration options:
    --with-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/Tony-MBP/.rvm/rubies/ruby-2.1.3/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}/lib

extconf failed, exit code 1

Gem files will remain installed in /Users/Tony-MBP/.rvm/gems/ruby-2.1.3/gems/pg-0.17.1 for inspection. 
Results logged to /Users/Tony-MBP/.rvm/gems/ruby-2.1.3/extensions/x86_64-darwin-13/2.1.0/pg-0.17.1/gem_make.out

Can anyone please help identify why both of these are failing?  Thanks for helping out a complete newb in his developer infancy. 
Edit:
After trying the suggested code from @Lidan, this is what I get:
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config'
    This could take a while...
    /Users/Tony-MBP/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/ext/builder.rb:73: warning: Insecure world writable dir /usr/local in PATH, mode 040777
    ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension. 

A:

After further research, OS X Mavericks wasn't playing nicely with pg or bundle gems.  
The four-step fix that worked for me:
brew update
brew install postgresql
gem install pg
bundle install

