Configurer Discourse pour utiliser un serveur PostgreSQL séparé

Supposons que vous utilisiez AWS RDS ou un serveur PostgreSQL existant géré ailleurs. Il est assez simple de configurer Discourse pour utiliser une telle configuration, sans avoir besoin d’accorder des privilèges de superutilisateur à tout ce que Discourse exécute lui-même. Voici comment.

Prérequis

Discourse exige que la version majeure de PostgreSQL corresponde à celle que nous fournissons dans nos images Docker ; si vous essayez d’exécuter sur une version plus récente ou plus ancienne, cela pourrait fonctionner, mais nous ne le garantissons pas, et si quelque chose casse à l’avenir, vous devrez gérer les deux parties. Nous exigeons également certaines extensions qui sont distribuées dans le paquet postgresql-contrib (ou son équivalent).

Générez aléatoirement un mot de passe pour la connexion (20 caractères ou plus). Dans ce guide, nous utiliserons s3kr1t comme espace réservé.

Configuration de la base de données

En tant que superutilisateur de la base de données, exécutez ce qui suit :

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

C’est tout ce qui doit être fait pour préparer la base de données à être utilisée par Discourse.

Configuration de Discourse

Configurez Discourse de la manière habituelle, puis modifiez containers/app.yml comme suit :

  1. Supprimez la ligne - "templates/postgres.template.yml" sous la clé de niveau supérieur templates (vers le haut du fichier) ;
  2. Sous la clé de niveau supérieur env, définissez ce qui suit :
        DISCOURSE_DB_USERNAME: discourse
        DISCOURSE_DB_PASSWORD: s3kr1t
        DISCOURSE_DB_HOST: <IP ou nom d'hôte>
        DISCOURSE_DB_NAME: discourse
  1. Vous devez également définir ce qui suit si votre base de données n’utilise pas le port par défaut (5432) :
       DISCOURSE_DB_PORT: <port>
       DISCOURSE_DB_BACKUP_PORT: <port>
  1. Reconstruisez avec ./launcher rebuild app, et vous devriez être prêt à partir.
39 « J'aime »
Install the docker version with a remote postgresql
Using a PGSQL database outside of Docker?
Installation on AWS
Is there a way to install Discourse without the provided scripts?
Error when attempting install
pgSQL root access - without? (docker)
Installation Help required
Database Replication
Improve my DO droplet setup
I want to keep PostgreSQL database on separate server [standard docker install]
Switching to another Redis instance?
Using Discourse with external PostgreSQL and Redis
How do you scale discourse horizontally?
Running Discourse with pg_bouncer and a separate DB
Can I install Discourse with my own Redis and Postgres?
How to provide HA
Simplified Discourse install?
Running discourse with Postgres running in docker
Search no longer works after 3.1.0.beta1 update
Move from standalone container to separate web and data containers
Move from standalone container to separate web and data containers
How to resolve Pups exec errors during Discourse bootstrap
Can Discourse Support a Shared Forum for Two Websites with Distinct Styling?
Can Discourse Support a Shared Forum for Two Websites with Distinct Styling?
Not using volumes (or automatically using S3) when hosting with Docker
Move from standalone container to separate web and data containers
Which versions of AWS Aurora Postgres are supported?
Why is "rebuild" so tightly coupled to container run status?
Move from standalone container to separate web and data containers
Can I install Discourse with my own Redis and Postgres?
Error while connecting database from aws postgresql 10.1
Relation "selected" does not exist with external Postgres
Error executing 'postInstallation': Purging temp files
Discourse Database Question/Support
Separate psql server / migrate
Improving Instance Performance (Megatopics, Database Size and Extreme Load)
pgSQL root access - without? (docker)
Multiple Daily Backups
Rebuilding gives db:migrate error due to broken plugin
Can I use a later version of Postgres than Discourse?
Trust level freeze
Multiple backup schedules
Error setting up with separate Redis
Migrate quickly to separate web and data containers
External postgres setup not working
Discourse Image Builder for Gitlab CI/CD Pipelines
Any other locations where database connection IP / hostname set other than DISCOURSE_DB_HOST in app.yml?
Moving postgres db to central db server: build error

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

Thanks

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.

2 « J'aime »

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

Thanks

2 « J'aime »

The discourse_docker repo is where all that lives.

2 « J'aime »

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: https://github.com/discourse/discourse_docker/search?utf8=✓&q=DISCOURSE_DB_PORT&type=

3 « J'aime »

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

3 « J'aime »

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?

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

1 « J'aime »

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.

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.

1 « J'aime »

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.

1 « J'aime »

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

What about that ?

No it doesn’t. 

5 « J'aime »

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

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)

Just that its managed & 1 less thing to worry about

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.

2 « J'aime »

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

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)