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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ A `socket` or `host` parameter can be specified to connect through a unix socket
DATABASE_URL="postgres://username:password@/database_name?socket=/var/run/postgresql"
```

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

```

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.


```sh
DATABASE_URL="postgres:///database_name?socket=/var/run/postgresql"
```


A `search_path` parameter can be used to specify the [current schema](https://www.postgresql.org/docs/13/ddl-schemas.html#DDL-SCHEMAS-PATH) while applying migrations, as well as for dbmate's `schema_migrations` table.
If the schema does not exist, it will be created automatically. If multiple comma-separated schemas are passed, the first will be used for the `schema_migrations` table.

Expand Down