Postgres doesn't seem to be running when running Discourse locally using Docker

For my company I need to develop some features against our Discourse instance’s REST API. For local development and testing I’ve been following Install Discourse for development using Docker successfully for quite some time now.

It’s been a few weeks since I’ve last been developing. Yesterday I wanted to fire up Discourse locally again so I did my usual git pull && d/boot_dev && d/bundle install && d/rails s. However, the last command d/rails s fails with the following error:

URGENT: Failed to initialize site default: ActiveRecord::ConnectionNotEstablished 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?

/home/discourse/.bundle/gems/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:69:in `rescue in new_client'
[...]

When I open a Terminal into the discourse_dev container and do a ls -la /var/run/postgresql/ I can see that there is indeed no .s.PGSQL.5432 socket in here.

Ok, so I thought maybe I shouldn’t have blindly done a git pull first, so I checked out tag v4.4.0 and even tag v3.3.0 and tried again. But I’m getting the exact same error.

It looks to me as if the Postgres Database is not running inside the discourse_dev container?

The weird thing is: If I clone the discourse repo into a new folder (yes, even the latest code in the main branch) and fire up Discourse using Docker I’m not getting this error and Discourse starts successfully.
But of course I have an empty database then which is a bit unfortunate. I understand that the data is persisted inside tmp/postgres, which I can copy over from the old folder into the new one. But I can’t do that anymore because I didn’t know that yesterday, and in my yesterday’s troubleshooting attempts I deleted every temporary folder and the node_modules and the .pnpm-store folder and so on. But the problem still persists.

Does anybody have a clue what is happening here or what I can do to troubleshoot this?

It seems something is utterly broken with my local data that prevents postgres from starting up.

When I sudo cp -pr discourse/data/postgres discourse2/data/postgres I get the exact same issue trying to boot Discourse from the discourse2 folder. Reversely, when I delete discourse/data/postgres my original discourse instance boots up fine, but without data of course :frowning:

Sorry for the trouble. This was likely due to the recent PostgreSQL 15 update. The old PG 13 data files are incompatible with the discourse_dev image which now runs PG 15, but our automated database update procedure was not intended for development environments.

Any particular reason why you want to retain the old data files?

You can run rake dev:populate inside the container to generate some test data.

1 Like

Don’t use that guide. It needs to be updated or deleted.

Use this one: Developing Discourse using a Dev Container

2 Likes

Oh this makes so much sense. Thanks so much for explaining. TIL it is a bad idea anyway to blindly do a git pull on the main branch since so much can be unintentionally break :slight_smile:

Only because of some admin settings, specifically regarding Discourse Connect. Also some REST API settings, some Plugin Settings and some specific manual user groups. We have a peculiar SSO setup in place which manages categories and user groups (+ group membership) via the REST API. Actual content as in posts and replies I do not care about at all during development which is why rake dev:populate wouldn’t help me, I assume.

No hard feelings here.