# Configure Discourse to use a separate PostgreSQL server

**URL:** https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375
**Category:** Self-Hosting
**Tags:** how-to
**Created:** [June 23, 2016, 11:48pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375 "2016-06-23T23:48:02Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Discourse](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/32/148734_2.png) [@Discourse](https://meta.discourse.org/u/Discourse)
#### Post date: [June 23, 2016, 11:48pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/1 "2016-06-23T23:48:02Z")

</div>

Let’s say you’re using AWS RDS, or an existing PostgreSQL server managed elsewhere. It’s fairly straightforward to get Discourse to use such a setup, without needing to grant superuser privileges to anything that Discourse itself runs. Here’s how.

## Prerequisites

Discourse requires PostgreSQL’s major version to [match what we ship with in our docker images](https://github.com/discourse/discourse_docker/blob/main/image/base/Dockerfile#L37); if you try to run on a newer or older version, it _might_ work, but we don’t guarantee it, and if something breaks in the future, you get to keep both pieces. We also require some extensions that are distributed in the `postgresql-contrib` package (or its equivalent). You will also need the `pgvector` extension (`postgresql-<version>-pgvector` package), which is required by the Discourse AI plugin that ships with Discourse core.

Randomly generate a password for the connection (20+ characters). In this guide, we’ll use `s3kr1t` as a placeholder.

## Setup the DB

As a database superuser, run the following:

```
CREATE USER discourse PASSWORD 's3kr1t';
CREATE DATABASE discourse OWNER discourse;
\c discourse
CREATE EXTENSION hstore;
CREATE EXTENSION pg_trgm;
CREATE EXTENSION unaccent;
CREATE EXTENSION vector;

```

That’s all that has to be done to prepare the DB for use by Discourse.

## Configure Discourse

Get Discourse setup in the usual way, and then edit `containers/app.yml` as follows:

1. Remove the line `- "templates/postgres.template.yml"` under the `templates` top-level key (towards the top of the file);

2. Under the `env` top-level key, set the following:

3. You also need to set the following if your database doesn’t use the default port (5432):

4. Rebuild with `./launcher rebuild app`, and you should be good to go.

> Last edited by @supermathie 2026-02-19T21:46:00Z
> 
> > **Check document**
> >
> > Perform check on document:

---

<div class="post-metadata">

### Author: ![alexweej](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexweej/32/120813_2.png) [@alexweej](https://meta.discourse.org/u/alexweej)
#### Post date: [July 27, 2017, 4:39pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/4 "2017-07-27T16:39:48Z")

</div>

There is an additional setting, `DISCOURSE_DB_PORT`, which can, naturally, configure the TCP port number. @mpalmer please update the post?

Thanks

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [July 27, 2017, 11:58pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/5 "2017-07-27T23:58:49Z")

</div>

This isn’t supposed to be a comprehensive description of every possible way you can configure your database connection, rather it’s a quick howto to make things a bit easier.

---

<div class="post-metadata">

### Author: ![alexweej](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexweej/32/120813_2.png) [@alexweej](https://meta.discourse.org/u/alexweej)
#### Post date: [July 28, 2017, 8:04pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/6 "2017-07-28T20:04:29Z")

</div>

Is there somewhere more authoritative for this documentation? I even tried (unsuccessfully) to track down the source code for this…!

Thanks

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [July 28, 2017, 11:42pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/7 "2017-07-28T23:42:15Z")

</div>

The [`discourse_docker` repo](https://github.com/discourse/discourse_docker) is where all that lives.

---

<div class="post-metadata">

### Author: ![alexweej](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexweej/32/120813_2.png) [@alexweej](https://meta.discourse.org/u/alexweej)
#### Post date: [July 31, 2017, 2:29pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/8 "2017-07-31T14:29:24Z")

</div>

Thanks for the pointer, I’m embarrassed to admit I’ve gone through almost an entire setup without realising the wealth of documentation on that page.

That said, the only reference to DISCOURSE\_DB\_PORT is in web.template.yml: [Repository search results · GitHub](https://github.com/discourse/discourse_docker/search?utf8=%E2%9C%93&q=DISCOURSE_DB_PORT&type=)

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [July 31, 2017, 11:58pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/9 "2017-07-31T23:58:14Z")

</div>

Yes, it could probably stand to be promoted into the default “web\_only” template, with a suitably explanatory comment. PRs welcome!

---

<div class="post-metadata">

### Author: ![Ivan\_Rapekas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivan_rapekas/32/248924_2.png) [@Ivan\_Rapekas](https://meta.discourse.org/u/Ivan_Rapekas)
#### Post date: [September 21, 2017, 9:25am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/10 "2017-09-21T09:25:26Z")

</div>

Hi, first thanks for this guide. I try to setup external postgresql on the same machine, where docker installed. Do I need to bind port of my external postgresql 5432 to docker? Could anybody assist how to do it, using Discourse configuration file?

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [September 21, 2017, 9:27am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/11 "2017-09-21T09:27:00Z")

</div>

Your question doesn’t make any sense. You don’t bind external listening ports to Docker.

---

<div class="post-metadata">

### Author: ![Ivan\_Rapekas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivan_rapekas/32/248924_2.png) [@Ivan\_Rapekas](https://meta.discourse.org/u/Ivan_Rapekas)
#### Post date: [September 21, 2017, 1:34pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/12 "2017-09-21T13:34:44Z")

</div>

I follow the instruction a the top. First I set up an ordinary docker, it works. Then I want to switch the application to external postgres. When I rebuild the app, I get an error

`Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?`

My server has not a FQDN yet, I use 127.0.0.1 as a DB host. The database is listening on 127.0.0.1:5432, I am able to connect with psql. Where am I wrong? I tried to play with pg\_hba.conf but no success, of course.

---

<div class="post-metadata">

### Author: ![Ivan\_Rapekas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivan_rapekas/32/248924_2.png) [@Ivan\_Rapekas](https://meta.discourse.org/u/Ivan_Rapekas)
#### Post date: [September 22, 2017, 5:44am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/13 "2017-09-22T05:44:44Z")

</div>

Hi guys, possible of my personal server configuration or what else.. But I found a solution by running a docker in hosted mode (not bridged as default):

```plaintext
/var/discourse/launcher rebuild app --docker-args --net=host --skip-mac-address

```

After that the discourse engine could connect to external postgres database, run at 127.0.0.1:5432. Default bridged mode [does not allow](https://stackoverflow.com/a/24326540/3013944) docker to access ports listening at the host without tricks.

Hope this might help somebody.

---

<div class="post-metadata">

### Author: ![davedogs](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@davedogs](https://meta.discourse.org/u/davedogs)
#### Post date: [March 14, 2018, 12:40am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/14 "2018-03-14T00:40:31Z")

</div>

When I run `./launcher rebuild app`, I get this error `PG::ConnectionBad: timeout expired` during the `bundle exec rake db:migrate`. Has anyone comes across this issue? I can successfully access the database with a postgres client.

---

<div class="post-metadata">

### Author: ![jpsayshello](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jpsayshello](https://meta.discourse.org/u/jpsayshello)
#### Post date: [June 22, 2018, 7:00pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/15 "2018-06-22T19:00:57Z")

</div>

I have not tried this yet.  
Here it is suggested to remove web.template.yml but i see bunch of other stuff happening in [discourse\_docker/templates/web.template.yml at master · discourse/discourse\_docker · GitHub](https://github.com/discourse/discourse_docker/blob/master/templates/web.template.yml)

What about that ?

---

<div class="post-metadata">

### Author: ![schleifer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schleifer/32/86416_2.png) [@schleifer](https://meta.discourse.org/u/schleifer)
#### Post date: [June 22, 2018, 8:33pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/16 "2018-06-22T20:33:30Z")

</div>

> [@jpsayshello](#):
>
> Here it is suggested to remove web.template.yml

No it doesn’t.&nbsp;

> [@mpalmer](#):
>
> Remove the line `- "templates/postgres.template.yml"`

---

<div class="post-metadata">

### Author: ![Tango](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tango/32/97926_2.png) [@Tango](https://meta.discourse.org/u/Tango)
#### Post date: [August 17, 2018, 6:13am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/17 "2018-08-17T06:13:48Z")

</div>

Can you take the same approach for Redis?

remove “templates/redis.template.yml” & add a line in the app.yml file for DISCOURSE\_REDIS\_HOST

Thanks in advance

Todd

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [August 17, 2018, 6:17am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/18 "2018-08-17T06:17:52Z")

</div>

Is there an obvious benefit that I’m maybe missing?

(I assume running redis on a separate machine will just add unwanted latency if nothing else)

---

<div class="post-metadata">

### Author: ![Tango](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tango/32/97926_2.png) [@Tango](https://meta.discourse.org/u/Tango)
#### Post date: [August 17, 2018, 6:18am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/19 "2018-08-17T06:18:47Z")

</div>

Just that its managed & 1 less thing to worry about

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [August 17, 2018, 6:19am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/20 "2018-08-17T06:19:43Z")

</div>

You don’t have to worry about redis that ships with discourse. I haven’t seen it failing on me in the last 1 year of self hosting discourse.

---

<div class="post-metadata">

### Author: ![Tango](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tango/32/97926_2.png) [@Tango](https://meta.discourse.org/u/Tango)
#### Post date: [August 17, 2018, 9:13pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/21 "2018-08-17T21:13:35Z")

</div>

Thanks @itsbhanusharma,

Do you know what the process would be even if I did?

This [article](https://web.archive.org/web/20140424075917/http://0ak.org/how-to-setup-discourse-on-amazon-aws/), albeit a little old says to add

DISCOURSE\_REDIS\_HOST

which is the same format as the [article](https://meta.discourse.org/t/running-discourse-with-a-separate-postgresql-server/46375) you so kindly mentioned, so that all makes sense.

Just not sure where to put it (maybe underneath other changes made to the app.yml file or maybe in the `"templates/redis.template.yml"`)

and also

what if any lines or files need to be deleted

Thanks

Todd

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [August 18, 2018, 7:00am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/22 "2018-08-18T07:00:29Z")

</div>

That variable should go into the file where you declared postgres variables. Just be warned that exposing redis for a production environment is lethal. (Because of poor security practices of redis itself)

[Next page](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375.md?page=2)
