-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add further common Postgres Unix socket URL examples #459
base: main
Are you sure you want to change the base?
Conversation
For passwordless authentication like Postgres [peer auth-method](https://www.postgresql.org/docs/current/auth-pg-hba-conf.html) the URL can be shortened: | ||
|
||
```sh | ||
DATABASE_URL="postgres://username/database_name?socket=/var/run/postgresql" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I would have expected needing at least postgres://username@/database_name?socket=/var/run/postgresql
(with @
, to differentiate it from a hostname)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i tested it with Postgres 14.9.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amacneil This only works with a local connection with the peer
authentication method, because in that specific mode, Postgres isn't using the hostname or username from the connection string, as it's connecting locally (so hostname is irrelevant) and peer
auth relies on Postgres getting the OS username, so the username from the connection string is irrelevant.
In other words, this works:
postgres@e8f183dbe0dd:/tmp$ ./dbmate -u postgres:///dbmate_test status
[ ] 20231115033218_test.sql
Applied: 0
Pending: 1
Notice that I'm doing this as user postgres
, which happens to be a valid database user. If I try this as user root
that isn't a valid user in this Postgres database, we get this error:
root@e8f183dbe0dd:/tmp# ./dbmate -u postgres:///dbmate_test status
Error: pq: role "root" does not exist
DATABASE_URL="postgres://username/database_name?socket=/var/run/postgresql" | ||
``` | ||
|
||
Additionaly, when the username and database name are identical the URL can be shortened further: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, this is also surprising to me. I guess it's postgres-specific behavior. It's not something dbmate does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, i tested it with Postgres 14.9.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually has nothing to do with the username and database name being identical. When relying on Postgres peer authentication method, specifying the username and hostname in the connection string are unnecessary:
Check this out:
postgres@e8f183dbe0dd:/tmp$ ./dbmate -u postgres://hostnameIsIgnoredWhenSocketIsUsed/dbmate_test?socket=/var/run/postgresql status
[ ] 20231115033218_test.sql
Applied: 0
Pending: 1
postgres@e8f183dbe0dd:/tmp$ ./dbmate -u postgres:///dbmate_test?socket=/var/run/postgresql status
[ ] 20231115033218_test.sql
Applied: 0
Pending: 1
I think adding these shorthand Postgres database URL examples to the documenation might be handy, but the current PR's explanation for why they work in the README are not accurate.
Add more and shorter URL syntax examples when no password is needed for the Unix socket connection.
I had struggled to understand shortened socket connections strings in Postgres and would have liked these examples to make it obvious that parts are simply left out.
Feel free to close or edit as you please, and thanks for this great tool.
edit: shameless bump @amacneil