# Running discourse with Postgres running in docker

**URL:** https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803
**Category:** Development
**Created:** [February 2, 2022, 6:21am UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803 "2022-02-02T06:21:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![abdellani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/abdellani/32/245154_2.png) [@abdellani](https://meta.discourse.org/u/abdellani)
#### Post date: [February 2, 2022, 6:21am UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/1 "2022-02-02T06:21:15Z")

</div>

Hi

I’m trying to set up discourse in my local development environment with only a small difference: I’m running Postgres in docker.

The database is accessible using psql from the OS, but when I tried to create the database using rake or rails, it doesn’t work. I followed these steps:

```plaintext
cp discourse_defaults.conf discourse.conf

```

then updated discourse.conf

```plaintext
...
db_host = 127.0.0.1
...
db_username = postgres
db_password = mysecretpassword
...

```

but when I try to run db:create

```plaintext
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?
Couldn't create 'discourse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Tasks: TOP => db:create
(See full trace by running task with --trace)

```

I tried with .env

```plaintext
DISCOURSE_DB_HOST=127.0.0.1
DISCOURSE_DB_USERNAME=postgres
DISCOURSE_DB_PASSWORD=mysecretpassword

```

I also tried with different IP addresses for ‘DB\_HOST’, but I’m still getting the same error.

Is there something that I’m missing?

---

<div class="post-metadata">

### Author: ![phil22](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phil22/32/248480_2.png) [@phil22](https://meta.discourse.org/u/phil22)
#### Post date: [February 2, 2022, 3:27pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/2 "2022-02-02T15:27:20Z")

</div>

I’m not certain, but I think the problem is probably that discourse is running inside a docker container, and so can’t by default access your separate postgres container.

I _think_ you need to expose the databse using this kind of configuration [GitHub - discourse/discourse\_docker: A Docker image for Discourse · GitHub](https://github.com/discourse/discourse_docker#expose)

You may also need to remove the postgres template from the app.yml file.

[These docs](https://meta.discourse.org/t/running-discourse-with-a-separate-postgresql-server/46375) might be helpful - whilst you’re not running on a separate server everything else should be relevant.

Hope this helps!

---

<div class="post-metadata">

### Author: ![abdellani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/abdellani/32/245154_2.png) [@abdellani](https://meta.discourse.org/u/abdellani)
#### Post date: [February 2, 2022, 4:02pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/3 "2022-02-02T16:02:05Z")

</div>

Hi @phil22

Thank you for your reploy.

Discourse is not running inside a docker container, it’s running in my OS. Postgres is running inside a container, and I exposed its port to the OS `-p 5432:5432`.

---

<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: [February 2, 2022, 4:03pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/4 "2022-02-02T16:03:40Z")

</div>

> [@abdellani](#):
>
> `PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory`

That means Discourse is trying to connect via a filesystem socket and that socket doesn’t exists.

Can you try adding a `port: 5432` to the `config/database.yml` file, under the development section?

---

<div class="post-metadata">

### Author: ![abdellani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/abdellani/32/245154_2.png) [@abdellani](https://meta.discourse.org/u/abdellani)
#### Post date: [February 2, 2022, 4:11pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/5 "2022-02-02T16:11:34Z")

</div>

Hi @Falco  
thank you for your reply.  
Same thing

```plaintext
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?
Couldn't create 'discourse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Tasks: TOP => db:create
(See full trace by running task with --trace)

```

---

<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: [February 2, 2022, 4:28pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/6 "2022-02-02T16:28:35Z")

</div>

Okay, try adding this line:

```plaintext
  url: postgresql://postgres::postgres@localhost:5432/discourse_development?pool=5

```

instead of the `port` one.

---

<div class="post-metadata">

### Author: ![abdellani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/abdellani/32/245154_2.png) [@abdellani](https://meta.discourse.org/u/abdellani)
#### Post date: [February 2, 2022, 4:32pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/7 "2022-02-02T16:32:38Z")

</div>

Thank you @Falco  
I tried to add all the missing information to database.yml, and now it’s working

```plaintext
#cat database.yml 
development:
  prepared_statements: false
  adapter: postgresql
  database: <%= ENV['DISCOURSE_DEV_DB'] || 'discourse_development' %>
  min_messages: warning
  port: 5432
  host: localhost
  user: postgres
  password: mysecretpassword
  pool: 5
  timeout: 5000

```

Does this mean that there is a problem in the loading of the configuration variables?

---

<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: [February 2, 2022, 4:34pm UTC](https://meta.discourse.org/t/running-discourse-with-postgres-running-in-docker/216803/8 "2022-02-02T16:34:23Z")

</div>

No it just means the the default `socket` takes priority of the `port` declaration. One way to get around that is using the `url` key, but you can check Rails code to see other ways.
