Connection to Postgres on Heroku with Go

2,121 views
Skip to first unread message

Matt Sherman

unread,
Oct 4, 2013, 12:09:42 AM10/4/13
to golan...@googlegroups.com
Has anyone successfully connected to Heroku's Postgres using a Go app? I am getting a consistent error: dial tcp 127.0.0.1:5432: connection refused. Locally, no such problem.

I've confirmed my ability to connect to the database via psql on heroku's command line, and have confirmed that the database url config is correct. The code is clear enough, so I wonder if there is a lower-level problem.

I am not asking you to troubleshoot for me (heck I work at Stack O), simply to get in touch if you've done Heroku + Go + Postgres successfully. Thanks!

===

The code, anyway, is:

func openDb() *sql.DB {
connection := os.Getenv("DATABASE_URL")

db, err := sql.Open("postgres", connection)
if err != nil {
log.Println(err)
}

return db
}

...and am importing github.com/lib/pq. Go version is 1.1.2.

Ian Ragsdale

unread,
Oct 4, 2013, 12:31:08 AM10/4/13
to golan...@googlegroups.com
The heroku database URL format is very different from the standard postgres spec format. Here's the code I use to translate:

regex := regexp.MustCompile("(?i)^postgres://(?:([^:@]+):([^@]*)@)?([^@/:]+):(\\d+)/(.*)$")
matches := regex.FindStringSubmatch(os.Getenv("DATABASE_URL"))
if matches == nil {
log.Fatalf("DATABASE_URL variable must look like: postgres://username:password@hostname:port/dbname (not '%v')", os.Getenv("DATABASE_URL"))
}

sslmode := os.Getenv("PGSSL")
if sslmode == "" {
sslmode = "disable"
}
spec := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", matches[1], matches[2], matches[3], matches[4], matches[5], sslmode)

Hope this helps.

Matt Sherman

unread,
Oct 4, 2013, 12:55:42 AM10/4/13
to golan...@googlegroups.com
Ah, glad to hear it's not just me, thanks for the help. I came up with a slightly different approach, which appears to succeed as well: http://stackoverflow.com/questions/19173460/connection-refused-with-go-postgres-on-heroku

Ian Ragsdale

unread,
Oct 4, 2013, 12:59:08 AM10/4/13
to Matt Sherman, golan...@googlegroups.com
Hmm, didn't see that ParseUrl method - looks like an improvement. Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/0XNrQRFuvoc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Maciek Sakrejda

unread,
Oct 4, 2013, 4:45:37 AM10/4/13
to golan...@googlegroups.com, Matt Sherman
On Thursday, October 3, 2013 9:59:08 PM UTC-7, Ian Ragsdale wrote:
Hmm, didn't see that ParseUrl method - looks like an improvement. Thanks!

Hi,

I've opened a pull request [1] to do this automagically in pq--there's really no reason it shouldn't, since psql/libpq support the URL format since 9.2.

I'm also surprised that you got such an opaque error; I'll see if we can improve that (I'm a pq maintainer; also I work on the Heroku Postgres team, although I think your problems here are not related to Heroku)

Thanks,
Maciek

[1]: https://github.com/lib/pq/pull/154

Matt Sherman

unread,
Oct 20, 2013, 3:06:26 PM10/20/13
to golan...@googlegroups.com, Matt Sherman
I've opened an issue to set the PGSSLMODE env variable in the go buildpack. Would this be helpful? Please add a comment in there, pro or con, if you have thoughts.
Reply all
Reply to author
Forward
0 new messages