Skip to content

Conversation

@brandur
Copy link
Collaborator

@brandur brandur commented Dec 24, 2024

Accept the standard set of PG* env vars as an alternative to database
configuration (e.g. PGHOST, PGDATABASE, etc.). This is mostly driven
by having done something similar for the CLI in [1], but was also
requested in #249. This turns out to be quite easy to do because pgx
does all the heavy lifting.

As noted in [1], a bonus of this is that it adds some additional
configuration options that aren't very easily doable right now, for
example around the use of an SSL certificate to connect to Postgres. We
get automatic support for these vars:

  • PGSSLCERT
  • PGSSLKEY
  • PGSSLROOTCERT
  • PGSSLPASSWORD

As part of this I also ended up rearranging some things in main.go.
Not strongly married to this design, but the idea is to get it into a
place where we can write tests for it, which previously wasn't possible.

Fixes #249.

[1] riverqueue/river#702


env:
# A suitable URL for the test database.
DATABASE_URL: postgres://postgres:postgres@127.0.0.1:5432/river_dev?sslmode=disable
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't needed anywhere so I took it out so that the env is cleaner for tests where we want to work with it (i.e. we don't have to unset DATABASE_URL before testing something).

Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I had already gotten to this, sorry about the delay! LGTM, maybe throw a changelog in?

Accept the standard set of `PG*` env vars as an alternative to database
configuration (e.g. `PGHOST`, `PGDATABASE`, etc.). This is mostly driven
by having done something similar for the CLI in [1], but was also
requested in #249. This turns out to be quite easy to do because pgx
does all the heavy lifting.

As noted in [1], a bonus of this is that it adds some additional
configuration options that aren't very easily doable right now, for
example around the use of an SSL certificate to connect to Postgres. We
get automatic support for these vars:

* `PGSSLCERT`
* `PGSSLKEY`
* `PGSSLROOTCERT`
* `PGSSLPASSWORD`

As part of this I also ended up rearranging some things in `main.go`.
Not strongly married to this design, but the idea is to get it into a
place where we can write tests for it, which previously wasn't possible.

Fixes #249.

[1] riverqueue/river#702
@brandur
Copy link
Collaborator Author

brandur commented Jan 4, 2025

Thanks. Changelog added.

@brandur brandur merged commit 37cb3cb into master Jan 4, 2025
13 checks passed
@brandur brandur deleted the brandur-pg-vars branch January 4, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

accept PG* env vars and making "DATABASE_URL" optional

3 participants