Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 631
displayer Avatar asked Jan 22 '26 10:01

displayer


1 Answers

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.

like image 110
Craig Ringer Avatar answered Jan 24 '26 21:01

Craig Ringer