ActiveRecord suppport authentication with non-postgres database #51547
+5
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
I'm using PostgreSQL and I don't allow any user to connect to it using
postgres
database. Instead all users need to connect using their own database.Currently Rails applications don't work with such configuration because of hardcoded
postgres
database that can't be changed. That is I set database inconfig/database.yml
but Rails will still attempt to connect usingpostgres
database.Detail
Use database from
config/database.yml
instead of hardcodedpostgres
.Note that there is one issue with this PR - what if the specified database in
config/database.yml
doesn't exist?I see 2 ways to solve that:
createdb $NAME
which is provided by PostgreSQL.postgres
database but note that even then our user might not haveCREATEDB
permissions.Additional information
Which users are allowed to connect with which databases can be configured in
pg_hba.conf
.For example config like:
Means that
rails
user is allowed to connect using Unix domain socket withrails
DB user usingrails
database. So connecting usingpostgres
database will be denied.Note that this doesn't affect database permissions and
rails
user might be allowed to query and modifypostgres
database even while being connected usingrails
database.It took me a while to hunt down this issue but basically application I was trying to setup (
rails db:setup
) was failing with:Notice the very misleading messages:
While in
config/database.yml
I havedatabase: rails
- really a hair pulling moment 🤯And also you can see there
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]