# 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:** 2

<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 18, 2018, 7:13am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/23 "2018-08-18T07:13:03Z")

</div>

Thanks

Anu reference or file need to be removed?

---

<div class="post-metadata">

### Author: ![hydrandt](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@hydrandt](https://meta.discourse.org/u/hydrandt)
#### Post date: [April 17, 2019, 5:34am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/24 "2019-04-17T05:34:51Z")

</div>

I needed to run development discourse (set up following [Install Discourse for development using Docker](https://meta.discourse.org/t/beginners-guide-to-install-discourse-for-development-using-docker/102009) ) using database from another container. To do so, I had to modify the installation steps as follows:

1. `git clone https://github.com/discourse/discourse.git`
2. `cd discourse`
3. `vim config/database.yml` , on the top of the file, make it into:

```plaintext
development:
  prepared_statements: false
  adapter: postgresql
  #database: <%= ENV['DISCOURSE_DEV_DB'] || 'discourse_development' %>
  database: discourse
  username: discourse
  password: yourdbpassword
  host: postgres
  min_messages: warning
  pool: 5
  timeout: 5000
  checkout_timeout: <%= ENV['CHECKOUT_TIMEOUT'] || 5 %>
  host_names:
    ### Don't include the port number here. Change the "port" site setting instead, at /admin/site_settings.
    ### If you change this setting you will need to
    ### - restart sidekiq if you change this setting
    ### - rebake all to posts using: `RAILS_ENV=production bundle exec rake posts:rebake`
    - "localhost"

```

1. `vim bin/docker/boot_dev`, find the line starting with `docker run`, and add a network definition matching the docker network to which your postgres container is attached to: `docker run --network my-docker_network-name -d -p 4305:...`
2. `./bin/docker/boot_dev`
3. `./bin/docker/unicorn`
4. you may need to run migrations: `docker exec -it discourse_dev /bin/bash -c "cd /src; ./bin/rails db:migrate RAILS_ENV=development"`
5. visit [http://localhost:9292/](http://localhost:9292/) and log in with credentials you set up earlier on that database

Is there a simpler way to do it, just with environment variables?

---

<div class="post-metadata">

### Author: ![aliyosefzai](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@aliyosefzai](https://meta.discourse.org/u/aliyosefzai)
#### Post date: [June 14, 2019, 3:26pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/25 "2019-06-14T15:26:07Z")

</div>

This failed for me. Any suggestions on how to fix it ?  
I am able to access DB discourse remotely from command line so the connection to DB looks good

```plaintext
FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 274 exit 1>
Location of failure: /pups/lib/pups/exec_command.rb:112:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
578a8ec702d0025b01a0b8396985b8bfc25c7029769c2960b58693c64609a62a
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one

```

---

<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: [June 14, 2019, 6:25pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/26 "2019-06-14T18:25:16Z")

</div>

I got the same error with the following lines in the log above:

```plaintext
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

```

Currently I am trying to find a fix.

---

<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: [June 14, 2019, 7:45pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/27 "2019-06-14T19:45:03Z")

</div>

The only way for me to rebuild with external postgres was the command mentioned earlier:

```
 rebuild app --docker-args --net=host --skip-mac-address

```

But in this case unicorn is started with default port 3000. Exposing ports is disabled as well. I cannot explain exactly, but something has changed since Sep '17, probably in launcher code.

---

<div class="post-metadata">

### Author: ![Wurzelseppi](https://avatars.discourse-cdn.com/v4/letter/w/eada6e/32.png) [@Wurzelseppi](https://meta.discourse.org/u/Wurzelseppi)
#### Post date: [June 26, 2019, 6:28am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/28 "2019-06-26T06:28:25Z")

</div>

Hey guys,

what´s the best practice to migrate from docker based postgre db to dedicated?  
The setup of the dedicated is described farther up this thread, right, but how can I then move the data from docker to dedicated db ?  
Probably with backup & restore, but is there a tutorial for that available ?

Thanks and greetings,

Julian

---

<div class="post-metadata">

### Author: ![thabbs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thabbs/32/82150_2.png) [@thabbs](https://meta.discourse.org/u/thabbs)
#### Post date: [June 26, 2019, 7:13am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/29 "2019-06-26T07:13:11Z")

</div>

Ideally, you have discourse stopped.  
Dump your database with pg\_dump and restore with pg\_restore.  
see [http://www.postgresqltutorial.com/postgresql-restore-database/](http://www.postgresqltutorial.com/postgresql-restore-database/)

Before starting up Discourse using the new database, issue as admin:

```
grant all privileges on database discourse to discourse;
alter schema public owner to discourse;
create extension if not exists hstore;
create extension if not exists pg_trgm;

```

---

<div class="post-metadata">

### Author: ![kkk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kkk/32/203533_2.png) [@kkk](https://meta.discourse.org/u/kkk)
#### Post date: [July 7, 2020, 12:26pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/30 "2020-07-07T12:26:55Z")

</div>

we can also use Hosts ip address instead of using `--net=host`  
172.17.0.1 is the default address for host machine from docker network on unix machines.  
Using `--net=host` restricts us to use `-p` option as docker argument.

```
DISCOURSE_DB_HOST = 172.17.0.1

```

---

<div class="post-metadata">

### Author: ![AlexanderK](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexanderk/32/203927_2.png) [@AlexanderK](https://meta.discourse.org/u/AlexanderK)
#### Post date: [September 22, 2021, 10:14am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/31 "2021-09-22T10:14:29Z")

</div>

Hi,  
Thank you for really great guide.  
Unfortunately I got an error while reproducing it.  
Initially I created discourse using launcher for all - app/redis/postgres. And it worked fine.  
But with external RDS launcher failed:

```plaintext
root@ip-172-31-42-129:/var/discourse# ./launcher rebuild app
Ensuring launcher is up to date
Fetching origin
Launcher is up-to-date
Stopping old container
+ /usr/bin/docker stop -t 60 app
app
cd /pups && git pull && git checkout v1.0.3 && /pups/bin/pups --stdin
docker: Error response from daemon: could not get container for discourse.xxxxxxxx.us-west-2.rds.amazonaws.com: No such container: discourse.xxxxxxxx.us-west-2.rds.amazonaws.com.
See 'docker run --help'.
cat: cids/app_bootstrap.cid: No such file or directory
"docker rm" requires at least 1 argument.
See 'docker rm --help'.

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers
rm: cannot remove 'cids/app_bootstrap.cid': No such file or directory
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.

```

Please advice how to resolve this issue.  
Thank you,  
Alexander K

---

<div class="post-metadata">

### Author: ![dotnetnate](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dotnetnate/32/198858_2.png) [@dotnetnate](https://meta.discourse.org/u/dotnetnate)
#### Post date: [November 2, 2021, 8:03pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/32 "2021-11-02T20:03:39Z")

</div>

Is there any way to pre-create and seed the database that doesn’t require going through the migration steps? We are running on AKS with external Postgres and the db setup seems to take what I consider to be an abnormally long amount of time (~8-9 minutes). Speeding this up would be great. Or is this a known issue in that configuration?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [November 2, 2021, 8:06pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/33 "2021-11-02T20:06:38Z")

</div>

That’s not a supported configuration.

If you’re building an image, a bunch more stuff than just the database is getting built like a bunch of templates are getting pre-compiled. I think that’s just how long it’s going to take.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [November 2, 2021, 8:52pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/34 "2021-11-02T20:52:52Z")

</div>

> [@dotnetnate](#):
>
> Is there any way to pre-create and seed the database that doesn’t require going through the migration steps?

Yes, you can bootstrap it elsewhere and then move the data to your production PostgreSQL.

However this will make updates very cumbersome.

---

<div class="post-metadata">

### Author: ![VinodSingh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinodsingh/32/259793_2.png) [@VinodSingh](https://meta.discourse.org/u/VinodSingh)
#### Post date: [November 17, 2022, 5:28am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/35 "2022-11-17T05:28:59Z")

</div>

Three things to look for.

One: by default PostgreSQL listens on `localhost`. Change the listen address in `postgresql.conf` file as shown below.

> listen\_addresses = ‘localhost,172.17.0.1’

Two: advise PostgreSQL to accept connections from docker images by adding following line to `pg_hba.conf` file.

> host all all 172.17.0.0/16 scram-sha-256

Restart PostgreSQL service after above two changes.

Three: If still unable to connect then check the firewall that might be blocking incoming connections on port `5432`.

---

<div class="post-metadata">

### Author: ![Samir\_Sabri](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/samir_sabri/32/287380_2.png) [@Samir\_Sabri](https://meta.discourse.org/u/Samir_Sabri)
#### Post date: [December 30, 2022, 8:42pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/36 "2022-12-30T20:42:34Z")

</div>

@Falco is there any risk to erase existing tables incase I used an existing DB ?  
Also, is there a way to add a prefix for discourse tables ?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 31, 2022, 1:11am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/37 "2022-12-31T01:11:10Z")

</div>

> [@Samir\_Sabri](#):
>
> is there any risk to erase existing tables incase I used an existing DB ?

No, unless they conflict with discourse names.

But I think it’s a bad idea.

> [@Samir\_Sabri](#):
>
> is there a way to add a prefix for discourse tables ?

No. I recommend that you use a separate database unless there is some reason they need to be connected.

What problem are you solving by sharing a database?

---

<div class="post-metadata">

### Author: ![Samir\_Sabri](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/samir_sabri/32/287380_2.png) [@Samir\_Sabri](https://meta.discourse.org/u/Samir_Sabri)
#### Post date: [December 31, 2022, 5:44am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/38 "2022-12-31T05:44:03Z")

</div>

Thanks, I am using AWS RDS, I just found that I can have multiple DBs on the same instance, thus, I’ve created a new DB with a new user

---

<div class="post-metadata">

### Author: ![linw-pnwe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/linw-pnwe/32/321942_2.png) [@linw-pnwe](https://meta.discourse.org/u/linw-pnwe)
#### Post date: [August 28, 2023, 3:46am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/39 "2023-08-28T03:46:33Z")

</div>

So I have a newly installed Discourse instance running via docker in a VM on Google Cloud. I currently have file uploads and discourse backups to buckets on Google Cloud enabled and those functions are working properly after following the instructions on the [Configure an S3 compatible object storage provider for uploads thread](https://meta.discourse.org/t/configure-an-s3-compatible-object-storage-provider-for-uploads/148916/339). I can see the test uploads in the bucket and when I look at upload URLs, all uploads are showing the proper URL from the CDN so they seem like they are pulling correctly from the bucket.

I then created a PostgreSQL 15.2 instance on Google Cloud and did the database setup procedure that’s outlined in the first post and configured the app.yml file as well. The default port for PostgreSQL on Google Cloud is 5432 so I left out those lines.  
If I use the public IP address of the postgres instance in the app.yml config, when I rebuild the app, I get the following:

```plaintext
FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 1024 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.1.1/lib/pups/exec_command.rb:117:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
bootstrap failed with exit code 1
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
a6a71b00bce378aa6334ae1c9fe103778d260bb699fe598f9685689e8b5ce450

```

Just to see what’s going on, I tried using the other IPs of the postgres instance.  
If I use the private IP address of the postgres instance, I get the following:

```plaintext
FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 1024 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.1.1/lib/pups/exec_command.rb:117:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
bootstrap failed with exit code 1
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
7333126c522eb51ace4d55ea89803eea54b96704baab70c322008cf2836ba47a

```

If I use the outgoing IP address of the postgres instance, I get the following:

```plaintext
FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 1026 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.1.1/lib/pups/exec_command.rb:117:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
bootstrap failed with exit code 1
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
c588d2b6977b9e7d493b0b59bc694369cb7c2219de67d5886112ed16312626ae

```

Using all the different IPs, the failed messages are all very similar and the postgres database does not receive any data or connections at all. Does anyone have any insight on what I’m doing wrong?

Also, is my issue being caused by not using the Cloud SQL Auth Proxy on the VM instance? If it is, I guess I’ll have to build a script to run the proxy and time it before the app rebuild. Does anyone have any insight on that?

Thanks for the time guys.

---

<div class="post-metadata">

### Author: ![linw-pnwe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/linw-pnwe/32/321942_2.png) [@linw-pnwe](https://meta.discourse.org/u/linw-pnwe)
#### Post date: [August 28, 2023, 4:17am UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/40 "2023-08-28T04:17:47Z")

</div>

I tried rebuilding a few more times switching among the IPs and it seems like the discourse database did eventually populate with tables. So now I’m even more stumped on what’s going on

---

<div class="post-metadata">

### Author: ![maggiomj](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@maggiomj](https://meta.discourse.org/u/maggiomj)
#### Post date: [September 20, 2023, 12:34pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/41 "2023-09-20T12:34:42Z")

</div>

could someone let me know what version of Discourse the original instructions were written for?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 20, 2023, 1:46pm UTC](https://meta.discourse.org/t/configure-discourse-to-use-a-separate-postgresql-server/46375/42 "2023-09-20T13:46:39Z")

</div>

It should work for an [standard install](https://meta.discourse.org/t/142537?silent=true) for the past 5 years or maybe forever.

Are you having a problem?

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

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