【问题标题】:Error installing pg - Setting up postgresql with activerecord(without rails)安装 pg 时出错 - 使用 activerecord 设置 postgresql(没有 rails)
【发布时间】:2019-02-06 05:36:48
【问题描述】:

需要帮助设置我的数据库应用程序,使用没有导轨的 activerecord。到目前为止,我已按照文档进行操作,但在安装 pg gem 时遇到了错误。这是在我的 environment.rb 中。

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

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

每次我在 CLI 上使用 sudo ARCHFLAGS="-arch x86_64" gem install pg 安装 postgres。我收到以下错误:

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

【问题讨论】:

    标签: ruby postgresql activerecord


    【解决方案1】:

    先安装postgreSQLdev包如下,

    sudo apt-get install libpq-dev
    

    或尝试,

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

    参考this post

    那么最常见的使用active_recordpostgres的方式如下,

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

    然后你可以定义类如下,

    class Post < ActiveRecord::Base
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-21
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 2016-01-24
      • 1970-01-01
      • 2017-08-02
      相关资源
      最近更新 更多