Running Discourse with a separate PostgreSQL server

(Matt Palmer) #1

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.

0. Prerequisites

Discourse requires PostgreSQL 9.5 or later; if you try to run on an 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).

1. Setup the DB

As a database superuser, run the following:

CREATE USER discourse PASSWORD 's3kr1t';
CREATE DATABASE discourse OWNER discourse;
\c discourse

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

2. 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:

     DISCOURSE_DB_HOST: <IP or hostname>
     DISCOURSE_DB_NAME: discourse
  3. Rebuild with ./launcher rebuild app, and you should be good to go.

Install the docker version with a remote postgresql
Installation with database and web on different servers
Unable to restore backup in web_only installation
Installation on AWS
How to move from standalone container to separate web and data containers
How to move from standalone container to separate web and data containers
(Alexander “Weej” Jones) #4

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


(Matt Palmer) #5

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.

(Alexander “Weej” Jones) #6

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


(Matt Palmer) #7

The discourse_docker repo is where all that lives.

(Alexander “Weej” Jones) #8

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: Search · DISCOURSE_DB_PORT · GitHub

(Matt Palmer) #9

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

(Ivan Rapekas) #10

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?

(Matt Palmer) #11

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

(Ivan Rapekas) #12

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 "" and accepting TCP/IP connections on port 5432?

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

(Ivan Rapekas) #13

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):

/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 Default bridged mode does not allow docker to access ports listening at the host without tricks.

Hope this might help somebody.


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.

(Jaimin) #15

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/web.template.yml at master · discourse/discourse_docker · GitHub

What about that ?

(Andrew Schleifer) #16

No it doesn’t.