Allow postgresql to use standard environment variables for connection (#3413)

This commit is contained in:
John W Higgins 2022-08-07 00:58:53 -07:00 committed by GitHub
parent 1a4bf0dcb5
commit 3afda0c89c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package postgres
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"strconv"
"time" "time"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -11,10 +12,6 @@ import (
"github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/util"
) )
const (
CONNECTION_URL_PATTERN = "host=%s port=%d sslmode=%s connect_timeout=30"
)
func init() { func init() {
filer.Stores = append(filer.Stores, &PostgresStore{}) filer.Stores = append(filer.Stores, &PostgresStore{})
} }
@ -56,7 +53,16 @@ func (store *PostgresStore) initialize(upsertQuery string, enableUpsert bool, us
UpsertQueryTemplate: upsertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) sqlUrl := "connect_timeout=30"
if hostname != "" {
sqlUrl += " host=" + hostname
}
if port != 0 {
sqlUrl += " port=" + strconv.Itoa(port)
}
if sslmode != "" {
sqlUrl += " sslmode=" + sslmode
}
if user != "" { if user != "" {
sqlUrl += " user=" + user sqlUrl += " user=" + user
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"strconv"
"time" "time"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -13,10 +14,6 @@ import (
"github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/util"
) )
const (
CONNECTION_URL_PATTERN = "host=%s port=%d sslmode=%s connect_timeout=30"
)
var _ filer.BucketAware = (*PostgresStore2)(nil) var _ filer.BucketAware = (*PostgresStore2)(nil)
func init() { func init() {
@ -61,7 +58,16 @@ func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableU
UpsertQueryTemplate: upsertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) sqlUrl := "connect_timeout=30"
if hostname != "" {
sqlUrl += " host=" + hostname
}
if port != 0 {
sqlUrl += " port=" + strconv.Itoa(port)
}
if sslmode != "" {
sqlUrl += " sslmode=" + sslmode
}
if user != "" { if user != "" {
sqlUrl += " user=" + user sqlUrl += " user=" + user
} }