Running discourse with Postgres running in docker

AI-generated summary

The user abdellani is trying to set up Discourse with Postgres running in a Docker container. Despite having access to the Postgres database using psql, they are unable to create the database using rake or rails.

abdellani has tried updating the discourse.conf file and setting environment variables, but still encounters the error connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory.

phil22 suggests that the issue might be due to the Postgres container not being exposed correctly, but this is not the case as abdellani has exposed the port.

Falco points out that Discourse is trying to connect to Postgres via a filesystem socket, which does not exist. Adding a port: 5432 to the database.yml file does not work, but adding a url key with the Postgres connection details does resolve the issue.

Falco explains that the issue is due to the default socket taking priority over the port declaration, and that using the url key is one way to get around this.

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:

cp discourse_defaults.conf discourse.conf

then updated discourse.conf

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

but when I try to run db:create

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

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?

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

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

These docs might be helpful - whilst you’re not running on a separate server everything else should be relevant.

Hope this helps!

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.

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?

Hi @Falco
thank you for your reply.
Same thing

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)

Okay, try adding this line:

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

instead of the port one.

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

#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?

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.