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
CREATE EXTENSION hstore;
CREATE EXTENSION pg_trgm;

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_USERNAME: discourse
     DISCOURSE_DB_PASSWORD: s3kr1t
     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
Installation on AWS
How to move from standalone container to separate web and data containers
Unable to restore backup in web_only installation
How to move from standalone container to separate web and data containers
Error when attempting install
Installation Help required
(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?

Thanks


(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…!

Thanks


(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 "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.


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

Hope this might help somebody.


#14

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. 


#17

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


(Bhanu Sharma) #18

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)


#19

Just that its managed & 1 less thing to worry about


(Bhanu Sharma) #20

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.


#21

Thanks @itsbhanusharma,

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

This article, albeit a little old says to add

DISCOURSE_REDIS_HOST

which is the same format as the article 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


(Bhanu Sharma) #22

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)