-
Notifications
You must be signed in to change notification settings - Fork 275
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
$PGHOST & $PGPORT are not respected. #492
Comments
This snippet exhibits the expected behavior described above, when using package main
import (
"database/sql"
"os"
"testing"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
)
func Test(t *testing.T) {
// $PGHOST is not defined in either the environment nor DATABASE_URL,
// so lib/pq will use a default value in its place.
t.Setenv("PGPORT", "16431")
t.Setenv("PGUSER", "some-user")
t.Setenv("PGPASSWORD", "some-password")
t.Setenv("DATABASE_URL", "postgres:///postgres?sslmode=disable")
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
row := db.QueryRow("SELECT current_database(), version()")
require.NoError(t, row.Err())
var dbName, version string
require.NoError(t, row.Scan(&dbName, &version))
assert.Equal(t, "postgres", dbName)
assert.NotEmpty(t, version)
} |
dbmate's pg driver doesn't use the environment variables as default connection parameter values the way the I don't know if this is so much a bug as it is a feature request for dbmate, but that's not especially important. This is how https://github.com/lib/pq/blob/3d613208bca2e74f2a20e04126ed30bcb5c4cc27/conn.go#L2002-L2082 I suppose if we're adding this capability in now, we should keep in mind what the latest Postgres 16 |
Given the following environment variables:
$DATABASE_URL
:postgres://
$PGHOST
:some-host
$PGPORT
:8888
$PGDATABASE
:some-db
I believe it'd be a logical assumption, for callers of
dbmate
, to expect that (dbmate
) would attempt, in any such case, to connect tosome-db
, via dialingsome-host
on port8888
, while retainingpostgres://
as the value of itsurl.URL
.However, what happens instead is that
dbmate
tries to connect via/var/run/postgresql/.s.PGSQL.5432
(or the equivalent platform default, as defined inconnectionString
), disregarding any Postgres-related environment variable that may be defined in its environment, and replaces the empty tokens (host
,port
, etc) with hard-coded values.I believe that
lib/pg
, the underlying driver, supports these environment variables when considering each connection string / DSN.Is this behavior intended? If not, happy to take a stab at a patch for it.
The text was updated successfully, but these errors were encountered: