Anyone running in to this (a year later!) using Rails 5.1.2 I did the following after first installing and initiating Postgresql (Centos 7).
So assuming you've already got postgresql installed and the postgres server&user set up.(+ the usual standard dev tools for Linux).
Add extra deps for rails to build gems.
$ sudo yum install postgresql-devel
Add postgres path in ~/.profile
export PATH=/usr/bin/postgres:$PATH (or your installed path)
Add another user/role with create db privileges using pgAdmin or shell
(should be the same user as the system/rails user because the postgres user doesn't have permissions for /rails/db/schema.rb, but the system/rails user does)
Below are shell commands for postgres create role and database.
$ sudo -u postgres psql (enter postgres password)
$ create role (linux/rails user) with createdb login password 'password';
$ \du (check its done and has createDB privs)
$ CREATE DATABASE name;
You will automatically become owner of the new database if no other arguments are presented.
Or you can use a gui like DBeaver to do the same.
So the above sets up for rails to access postgresql and build the pg gem once you've swapped out the default Gemfile & config/database.yml
Now create app and set it up (no need for -d postgresql flag because we swap out the Gemfile and the config/database.yml file contents completely and rails will install a postgresql db on bundle update/install.
(change some to rake for earlier versions of rails)
$ rails new app
$ cd app
$ atom (or editor) Gemfile config/database.yml
Swap out both file contents (to ones shown below) & save.
$ bundle update
$ bundle install
Check it with
$ rails db:create
(postgresql database should now be connected), so scaffold something
$ rails g scaffold Users name:string email:string comment:text
$ rails db:migrate
$ rails server
http://localhost:3000 shows the default page and http://localhost:3000/users brings up your new Users page using postgresql not sqlite3. Put something in to test it.
Below are the Gemfile and config/database.yml files I used for Rails 5.1.2, including taps for Heroku.
Gemfile rails 5.1.2
source 'https://rubygems.org'
gem 'rails', '5.1.2'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.7.0'
gem 'taps'
#Postgresql Database
group :production do
gem 'pg', '0.21.0'
end
group :development, :test do
gem 'sqlite3', '1.3.13'
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.0.8'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
config/database.yml file contents (watch out for indentation)
development:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
test:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
production:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
Doing all the above will get you a dev/prod postgresql database in rails but no test, you can also access the database directly from rails by just running "rails db" and entering password.
Its simple but gets you up and running with rails/postgresql quickly.