max connections limit lib/pq

1,366 views
Skip to first unread message

Tieson Molly

unread,
Jun 21, 2017, 8:56:14 AM6/21/17
to golang-nuts
I am running into an issue I have seen mentioned on here years ago. I am seeking the current best solution.

I have a web server connecting to a postgresql database using the standard database/sql and the lib/pq driver. I keep running out of connections over time as it seems the pool grows up to 100.

Is there a way to prevent or fix this?

Best regards,

Ty

Tamás Gulácsi

unread,
Jun 21, 2017, 4:58:31 PM6/21/17
to golang-nuts
You have to carefully close every Rows, Stmt, Tx and Conn acquired by database/sql.

Franco Marchesini

unread,
Jun 30, 2017, 10:22:11 AM6/30/17
to golang-nuts
If you really have over 100 simultaneous connection you need to configure postgresql engine
Regards
Franco

gerald...@gmail.com

unread,
Jun 30, 2017, 2:40:36 PM6/30/17
to golang-nuts
what you want is to set SetMaxOpenConns, SetMaxIdleConns:

db, err := sql.Open(config.DriverName, GetDataSourceFromConfig(config))
if err != nil {
log.Fatal(err)
}

db.SetMaxOpenConns(config.MaxOpenConns)
db.SetMaxIdleConns(config.MaxIdleConns)

Franco Marchesini

unread,
Jul 1, 2017, 2:45:07 AM7/1/17
to golang-nuts
Postgres default max connection is set to 100.


max_connections (integer)

Determines the maximum number of concurrent connections to the database server. The default is typically 100 connections, but might be less if your kernel settings will not support it (as determined during initdb). This parameter can only be set at server start.

When running a standby server, you must set this parameter to the same or higher value than on the master server. Otherwise, queries will not be allowed in the standby server.



Reply all
Reply to author
Forward
0 new messages