Coder Social home page Coder Social logo

Comments (2)

azazeal avatar azazeal commented on July 17, 2024

This snippet exhibits the expected behavior described above, when using lib/pq directly:

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)
}

from dbmate.

dossy avatar dossy commented on July 17, 2024

dbmate's pg driver doesn't use the environment variables as default connection parameter values the way the libpq C client library does, but I think that would be a useful enhancement, if you'd like to contribute it.

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 lib/pq handles supporting the environment variables, which may be interesting:

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 libpq C client library documents as available vs. what the underlying lib/pq Go client library supports, both as environment variables and connection string parameter keys. Ultimately, we can only support what the underlying lib/pq implements, and not what the libpq C client documents, so that's important to be aware of.

from dbmate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.