Gem Install Pg for Ruby on Rails

Add `gem 'pg'` to your Gemfile and ensure its version is at the minimum required by ActiveRecord

When you create a rails 5 application with

rails new app-name -d postgresql

it will set postgres gem (pg) to 0.21 by default.

Possibly you've skipped this step when you started your project, and added pg on later stages. Let's rewind.

rails new without-db #defaults to sqlite3
cd without-db

Then I've edited my Gemfile replacing

gem 'sqlite3'

to

gem 'pg'

then

bundle install

Now, postgres gem version is 1.0.0. Edited my database.yml for connection credentials.Then some stuff to generate model, migration, controller etc. and push them to heroku.

$ rails g scaffold book title:string author:string genre:string description:string
...
$ rake db:create
...
$ rake db:migrate
...
$ heroku login
...
$ git init
...
$ heroku git:remote -a without-db-sample
...
$ git add .
...
$ git commit -m "initial commit"
...
$ git push heroku master
...
$ heroku run rake db:migrate
...
$ heroku open

Albeit I don't love this version of pg, it worked normally.

Then edited Gemfile again, replacing,

gem 'pg' 

to

gem 'pg', '~> 0.18'

then

$ bundle install
...
$ bundle show pg
/home/ziya/.rvm/gems/ruby-2.4.1/gems/pg-0.21.0
$ git status
...
$ git add .
...
$ git commit -m "change postgres version"
...
$ git push heroku master

It still works normally - https://without-db-sample.herokuapp.com/books

gem install pg for ruby on rails

This basically means that you're missing a library or program on your setup. I've had this issue a couple of times now so here are a few different things to try (hopefully one of them will work for you):

Install a common missing library:

brew install libpqxx
gem install pg

Reinstall postgres (not ideal I know):

brew uninstall postgresql
brew install postgresql
gem install pg

Explicitly define path of pg_config:

1) Get location of pg_config

which pg_config

2) pass that path into gem install

gem install pg --with-pg-config=/usr/pgsql-9.1/bin/pg_config

Ruby on Rails 'pg' gem installation error Windows 10

I'm assuming you've downloaded and installed PostgreSQL on your system. Use the following to point the gem to where postgres is installed. This is an example of what it'd look like on my system.

gem install pg -- --with-pg-config='C:\Program Files\PostgreSQL\13\bin\pg_config.exe'

How to add postgresql to the gem file?

It works after I delete:

group :development, :test do
gem 'sqlite3'
end

group :production do
gem 'pg'
end

and simply add:

gem 'pg'

to my gem file.
finally run:

gem install bundler
git add .
git commit -m"blahblah"
git push heroku master

Thanks to Mikhail Katrin

Can't install pg gem on Windows

The message you're getting is a clear indication that you lack something for the correct installation of that gem:

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.

There is no Windows native version of latest release of pg (0.10.0) released yesterday, but if you install 0.9.0 it should install binaries without issues.

Anyhow, if you want to install the gem, you need a build environment installed. If you're using RubyInstaller, then you need the DevKit

Installation of the gem will only require you provide additional options to gem installation (like --with-pg-dir)

subst X: "C:\Program Files (x86)\PostgreSQL\8.3"
gem install pg -- --with-pg-dir=X:
subst X: /D


Related Topics



Leave a reply



Submit