Skip to content

Commit b52cb83

Browse files
committed
Supports alternate postgres:// prefix in URLs
Adds support for the alternate postgres:// prefix in URLs. It's maybe not the cleanest approach, but works. Hoping I can either get some pointers on a more appropriate patch, or that we could use this in the interim to unblock this use-case. Signed-off-by: Jack Wink <[email protected]>
1 parent 2391ad1 commit b52cb83

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cmd/postgres_exporter/datasource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (e *Exporter) discoverDatabaseDSNs() []string {
3535
var dsnURI *url.URL
3636
var dsnConnstring string
3737

38-
if strings.HasPrefix(dsn, "postgresql://") {
38+
if strings.HasPrefix(dsn, "postgresql://") || strings.HasPrefix(dsn, "postgres://") {
3939
var err error
4040
dsnURI, err = url.Parse(dsn)
4141
if err != nil {

config/dsn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (d DSN) GetConnectionString() string {
6565
// dsnFromString parses a connection string into a dsn. It will attempt to parse the string as
6666
// a URL and as a set of key=value pairs. If both attempts fail, dsnFromString will return an error.
6767
func dsnFromString(in string) (DSN, error) {
68-
if strings.HasPrefix(in, "postgresql://") {
68+
if strings.HasPrefix(in, "postgresql://") || strings.HasPrefix(in, "postgres://") {
6969
return dsnFromURL(in)
7070
}
7171

config/dsn_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ func Test_dsnFromString(t *testing.T) {
186186
},
187187
wantErr: false,
188188
},
189+
{
190+
name: "Alternative URL prefix",
191+
input: "postgres://user:[email protected]:5432/tsdb?user=postgres",
192+
want: DSN{
193+
scheme: "postgres",
194+
host: "host.example.com:5432",
195+
path: "/tsdb",
196+
query: url.Values{},
197+
username: "user",
198+
password: "s3cret",
199+
},
200+
wantErr: false,
201+
},
189202
{
190203
name: "URL with user and password in query string",
191204
input: "postgresql://host.example.com:5432/tsdb?user=postgres&password=s3cr3t",

0 commit comments

Comments
 (0)