Discourseを別のPostgreSQLサーバーを使用するように設定する

AWS RDS や、外部で管理されている既存の PostgreSQL サーバーを使用していると仮定します。Discourse 自体が実行するものにスーパーユーザー権限を付与する必要なく、そのようなセットアップを使用するように Discourse を設定するのはかなり簡単です。方法は次のとおりです。

前提条件

Discourse は、PostgreSQL のメジャーバージョンが Docker イメージで同梱されているものと一致する必要があります。より新しいバージョンまたは古いバージョンで実行しようとすると、動作する可能性はありますが、保証されません。将来的に何かが壊れた場合、両方のピースを保持することになります。また、postgresql-contrib パッケージ(またはその同等品)に含まれるいくつかの拡張機能も必要です。

接続用のパスワードをランダムに生成します(20文字以上)。このガイドでは、プレースホルダーとして s3kr1t を使用します。

DB のセットアップ

データベースのスーパーユーザーとして、以下を実行します。

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

Discourse で DB を使用できるようにするために必要なのはこれだけです。

Discourse の設定

通常どおり Discourse をセットアップしてから、containers/app.yml を次のように編集します。

  1. ファイルの先頭付近にある templates トップレベルキーの下の行 - "templates/postgres.template.yml" を削除します。
  2. env トップレベルキーの下で、以下を設定します。
        DISCOURSE_DB_USERNAME: discourse
        DISCOURSE_DB_PASSWORD: s3kr1t
        DISCOURSE_DB_HOST: <IP またはホスト名>
        DISCOURSE_DB_NAME: discourse
  1. データベースがデフォルトポート (5432) を使用していない場合は、以下を設定する必要もあります。
       DISCOURSE_DB_PORT: <ポート>
       DISCOURSE_DB_BACKUP_PORT: <ポート>
  1. ./launcher rebuild app で再構築すれば、準備完了です。
「いいね!」 39
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

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

Thanks

「いいね!」 2

The discourse_docker repo is where all that lives.

「いいね!」 2

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

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

「いいね!」 3

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

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

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

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

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

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)