How to enable Postgresql password-less authentication on Ubuntu for Rails local development

Fire up your console and create a Postgres user (aka role) named rails:

ಠ_ಠ sudo -u postgres psql
psql (9.3.4)
Type "help" for help.

postgres=# CREATE ROLE rails LOGIN
postgres-#   CREATEDB
postgres-#    VALID UNTIL 'infinity';

Then make sure your Postgres configuration uses the trust authentication.

First, locate the config file. Its path is usually /etc/postgresql/VERSION/main/pg_hba.conf.

Edit it as the superuser:

sudo vim /etc/postgresql/9.3/main/pg_hba.conf

or in a version independent way:

sudo vim /etc/postgresql/$(/etc/postgresql/ | grep -E "[0-9]+\.[0-9]+" | sort | tail -n 1)/main_pg_hba.conf

(I know, this one is a bit messy ^_^‘..)

And then, change the occurrences of peer and md5 to trust, like so:

# Database administrative login by Unix domain socket
local all postgres trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust

Save the file and restart the service:

ಠ_ಠ sudo service postgresql restart

Then edit the config/database.yml of your Rails application:

development:
  adapter: postgresql
  encoding: unicode
  database: MYAPPNAME_development
  host: localhost
  pool: 5
  username: rails

test:
  adapter: postgresql
  encoding: unicode
  database: MYAPPNAME_test
  pool: 5
  username: rails
  host: localhost

You can now perform all the database specific tasks:

ಠ_ಠ bundle exec rake db:migrate
ಠ_ಠ bundle exec rails server
# and so on..
 
11
Kudos
 
11
Kudos

Now read this

Riffs Hounds

Our contribute for the 2014 Rails Rumble can be found at riffshounds.olisti.co and you can vote us here. The team # We’re such a cute team, aren’t we? ;-) What have we done? # These are the main parts of our little game. The Playlist #... Continue →