---
"title": "Making the Most of PostgreSQL Databases on Heroku"
"description": "Postgres is our favorite database, it’s reliable and powerful; here are a few essential tips that can help you use Postgres more successfully."
"date": "2016-12-08"
"authors":
  -
    "name": "Timothée Peignier"
"url": "https://wp-www-staging.heroku.com/blog/postgres-essentials/"
"categories":
  - "News"
"tags":
  - "Heroku Postgres"
---

# Making the Most of PostgreSQL Databases on Heroku

Postgres is our favorite database—it’s reliable, powerful and secure. Here are a few essential tips learned from building, and helping our customers build, apps around Postgres. These tips will help ensure you get the most out of Postgres, whether you’re running it on your own box or using the [Heroku Postgres](https://wp-www-staging.heroku.com/postgres/) add-on.

## [Use a Connection Pooler](#use-a-connection-pooler)

Postgres [connections are not free](https://devcenter.heroku.com/articles/heroku-postgres-plans#plan-tiers), as each established connection has a cost. By using a connection pooler, you’ll reduce the number of connections you use and reduce your overhead. Most Postgres client libraries include a built-in connection pooler; make sure you’re using it. You might also consider using our [pgbouncer buildpack](https://github.com/heroku/heroku-buildpack-pgbouncer) if your application requires a large number of connections. PgBouncer is a server-side connection pooler and connection manager that goes between your application and Postgres. Check out some of our documentation for using PgBouncer for [Ruby](https://devcenter.heroku.com/articles/concurrency-and-database-connections#limit-connections-with-pgbouncer) and [Java](https://devcenter.heroku.com/articles/database-connection-pooling-with-java#limit-connections-with-pgbouncer) apps.

## [Set an Application Name](#set-an-application-name)

Postgres allows you to see what clients are connected and what each of them is doing using the built-in `pg_stat_activity` table. By explicitly marking each connection you open with the name of your dyno, using the `DYNO` environment variable, you’ll be able to track what your application is doing at a glance:

```
SET application_name TO 'web.1';
```

Now, if you will be to quickly see what each dyno is doing, using `heroku pg:ps`:

```
$ heroku pg:ps
procpid |         source            |   running_for   | waiting |         query
---------+---------------------------+-----------------+---------+-----------------------
   31776 | web.1      | 00:19:08.017088 | f  | <IDLE> in transaction
   31912 | worker.1   | 00:18:56.12178  | t  | select * from customers;
(2 rows)
```

You will also be able to see how many connections each dyno is using, and much more, by querying the `pg_stat_activity` table:

```
$ heroku pg:psql
SELECT application_name, COUNT(*) FROM pg_stat_activity GROUP BY application_name ORDER BY 2 DESC;
      application_name      | count 
----------------------------+-------
 web.1         |     15
 web.2         |     15
 worker.1      |     5
(3 rows)
```

## [Set a `statement_timeout` for Web Dynos](#set-a-code-statement_timeout-code-for-web-dynos)

Long running queries can have an impact on your database performance because they may hold locks or over-consume resources. To avoid them, Postgres allows you to set a [timeout](https://www.postgresql.org/docs/current/runtime-config-client.html) per connection that will abort any queries exceeding the specified value. This is especially useful for your web dynos, where you don’t want any requests to run longer than your [request timeout](https://devcenter.heroku.com/articles/request-timeout).

```sql
SET statement_timeout TO '30s';
```

## [Track the Source of Your Queries](#track-the-source-of-your-queries)

Being able to determine which part of your code is executing a query makes optimization easier, and makes it easier to track down expensive queries or n+1 queries. There are many ways to track which part of your code is executing a query, from a monitoring tool like [New Relic](https://elements.heroku.com/addons/newrelic) to simply adding a comment to your SQL specifying what code is calling it:

```sql
SELECT  `favorites`.`product_id` FROM `favorites` -- app/models/favorite.rb:28:in `block in <class:Favorite>'
```

You will now be able to see the origin of your [expensive queries](https://devcenter.heroku.com/articles/expensive-queries), and be able to track down the caller of the query when using the `pg_stat_statements` and `pg_stat_activity` tables:

```
$ heroku pg:psql
SELECT (total_time/sum(total_time) OVER()) * 100 AS exec_time, calls, query FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;
----------------------------------------------------------------------------------------------------------------------------
exec_time | 12.2119460729825
calls     | 7257
query     | SELECT  `favorites`.`product_id` FROM `favorites` -- app/models/product.rb:28:in `block in <class:Product>'
```

Many ORMs provide this feature [built-in](https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-logging) or via [extensions](https://github.com/cyberdelia/sequel-caller-location), make sure you use it and your debugging and optimization will be easier.

## [Learn More](#learn-more)

There is much more you can learn about Postgres, either via the excellent [documentation](https://www.postgresql.org/docs/current/index.html) of the project itself, or the Heroku Postgres [Dev Center reference](https://devcenter.heroku.com/categories/heroku-postgres). Share your own tips with the community on the \#postgrestips hashtag.