displayer
displayer

Reputation: 73

Rails connects to database without username or password

I'm developing a web using Rails 4. Trying to configure the production environment I noticed that neither the username nor the password are needed. I have the following configuration under database.yml:


development:
  adapter: postgresql
  encoding: unicode
  database: walko
  username: walko
  password:
  pool: 5
  timeout: 5000

production: adapter: postgresql encoding: unicode database: <%= ENV['DB_NAME'] %> username: password: pool: 5 timeout: 5000

I can realize migrations and connections to the production database from rails console.

How is this possible?

Upvotes: 3

Views: 1927

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324531

Rails uses the Ruby Pg gem, which uses PostgreSQL's libpq client library.

libpq uses the current system user name if no user name is supplied.

No password is required by the server if peer, ident sameuser or trust authentication is used in pg_hba.conf. If the server doesn't request a password, libpq doesn't try to send one and doesn't care if one is missing.

You can actually supply the empty string for the database too; libpq will then use the db that's the same as your username if it exists.

Upvotes: 13

Related Questions