Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

malteneuss
Copy link

@malteneuss malteneuss commented Jul 13, 2023

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

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"
Copy link
Owner

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)

Copy link
Author

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.

Copy link
Collaborator

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:
Copy link
Owner

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.

Copy link
Author

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.

Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants