29

Hi I'm trying to hook up postgresql to my rails project. I'm learning testing but my tests aren't running because of a postgresql error about having an incorrect password:

Edmunds-MacBook-Pro:langexchange edmundmai$ rake test:units
rake aborted!
fe_sendauth: no password supplied

I've already read around and my pg_hba.conf file was originally already like this:

# 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
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     edmundmai  

here's my database.yml file in my rails project

default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  username: edmundmai
  password: 

development:
  <<: *default
  database: project_development

test:
  <<: *default
  database: project_test

production:
  <<: *default
  database: project_production

Does anyone know how I could fix this? We can chat if it's easier.

bigpotato
  • 26,262
  • 56
  • 178
  • 334

4 Answers4

22

Be sure that you have defined both RAILS_ENV and RACK_ENV on your production hosting server. I had a similar issue crop up where everything worked except rake and the solution was to add RACK_ENV=production to my environment. RAILS_ENV was already set to production.

export RACK_ENV=production

bundle exec rake db:version

You should see some output such as:

Current version: 20121126151935

Solution: make sure your Rails app has access to the RAILS_ENV and RACK_ENV environmental variables. Also, ensure they're both the same value.

danielricecodes
  • 3,446
  • 21
  • 23
  • 2
    Thanks! Just switched from capistrano to another deployment solution. Apparently, capistrano takes care of RACK_ENV and RAILS_ENV in the background. I suppose I would've learned more without all the magic. :D – jrhorn424 Sep 19 '13 at 02:25
  • @Edmund, It would be great to make this the accepted answer. It has more upvotes than the answer above :) – danielricecodes Jun 09 '14 at 19:42
  • export RACK_ENV=production worked for me, however I'm using Passenger with Apache and according to the documentation Passenger is already setting RACK_ENV=production? This is my integration testing machine so I don't want to set a global RACK_ENV, feels like I'm missing somthing. – Lee Jul 11 '14 at 11:31
11

First you should change:

local   all             all                                     trust

to:

local   all             all                                     md5

Then, You should create a super_user in PostgreSQL with username and password,after that adding username and password of new user to your database.yml file.

To create new super_user, open Pg Admin III and right click at the bottom Login Roles to create new super user.

Thanh
  • 8,219
  • 5
  • 33
  • 56
  • hey kien. where do I find Pg Admin III? I've never heard of that before can you teach me how – bigpotato Dec 03 '12 at 19:41
  • Did you install PostgreSQL? After you installed it, you will have that tool. Search in your apps. – Thanh Dec 03 '12 at 19:43
  • yeh I used homebrew to install postgres. Where exactly do I find Pg Admin III? – bigpotato Dec 03 '12 at 19:45
  • 1
    I think I found it. It's a program in my computer right? But I don't see the login roles buttons you're talking about. – bigpotato Dec 03 '12 at 19:48
  • yes, it's in your computer. Double click on local (localhost) server and scroll to the bottom, you will see it. – Thanh Dec 03 '12 at 19:50
  • I think the issue is that I don't have any local servers... But when I try to create one with a name, username, password, it always gives me the error: `Error connecting to the server: FATAL: password authentication failed for user "whatever_username_i_entered"` – bigpotato Dec 03 '12 at 19:52
  • try create a server with: Name: local, host: localhost, port: 5432, MaintenanceDB: postgres, username: postgres – Thanh Dec 03 '12 at 19:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20524/discussion-between-kien-thanh-and-edmund) – Thanh Dec 03 '12 at 20:01
  • Where do we have to change 'local all all md5' ? –  Jan 04 '18 at 21:29
4

On your local machine under the test and development sections of the database.yml file you should change the username to your logged in username. The rails generator makes it the same as your appname and that will not work. The production section will depend on your production setup. If you use heroku you do not need to change anything there.

andreofthecape
  • 1,166
  • 11
  • 24
0

You need only to create the user/passw and schema(db) in postgres with pgadmin and put user/passw in database.yml. When I tryed to create database with rake db:create in current rails project folder, it failed, and trowed to me error could't create for postgreSQL adapter. After that I created the schema for rails project with pgadmin, everyrthing is fine without any changes in pg_hba.conf.

Kras
  • 1