1

Need help setting up my database application with activerecord without rails. I have followed the documentation so far and I am encountering an error in regards to installing the pg gem. This is inside of my environment.rb.

require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
require 'active_record'

ActiveRecord::Base.establish_connection("postgres://localhost/development")
require_all 'app'

Every time I install postgres on the CLI with sudo ARCHFLAGS="-arch x86_64" gem install pg. I get an error of:

Building native extensions. This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    current directory: /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/pg-1.1.4/ext
/Users/kenkuts/.rvm/rubies/ruby-2.5.3/bin/ruby -r ./siteconf20190206-73835-1tfhfez.rb 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/kenkuts/.rvm/rubies/ruby-2.5.3/bin/$(RUBY_BASE_NAME)
    --with-pg
    --without-pg
    --enable-windows-cross
    --disable-windows-cross
    --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

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/pg-1.1.4 for inspection.
Results logged to /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0/pg-1.1.4/gem_make.out

1 Answer 1

2

Install first install postgreSQL dev package as below,

sudo apt-get install libpq-dev

or try with,

gem install pg  --   --with-pg-lib=/usr/lib   

referring this post

Then most common way to use active_record along with postgres is as below,

require 'active_record'
require 'pg'

ActiveRecord::Base.establish_connection(
 # database schema i.e. database, encoding, username, password etc.
)

Then you can define class as below,

class Post < ActiveRecord::Base
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.