Hey, I started to build main page for my site. It’s on same server as Discourse of course. I need to access container’s PostgreSQL server from host to pull out Discourse posts from category “News” on my main page. How do I connect to it?
expose PostgreSQL port from docker image and you can access it.
Sounds easy. Is it safe though? Won’t this expose port for external connections too?
Yes. It will too. But you can prevent it by using iptables or firewall (I don’t really know, still learning about linux)
Or expose the port on 127.0.0.1, instead of 0.0.0.0.
You mean, I can do like this in
expose: - "127.0.0.1:4002:5432" # fwd host port 4002 to container port 5432 (postgres) - "127.0.0.1:4001:80" # fwd host port 80 to container port 80 (http) - "127.0.0.1:2222:22" # fwd host port 2222 to container port 22 (ssh)
Discourse API Documentation
not 100% sure but we copy it to docker run so in theory that may work, try it … you can do a
docker ps to see if it worked as expected
then report back.
I didn’t tried it with Postgres yet, but I just hooked http like this:
127.0.0.1:4001:80 and it works. Now I can’t connect to Discourse via
http://mysite:4001. It should work with everything else, thanks!
Hey. Two quick questions:
- If I’ll create my own tables inside of Discourse DB, will they survive rebuilding of container?
- Will Discourse backup my tables inside of Discourse DB?
I think it’s a waste of resourses to host another PostgreSQL server or something else if there’s perfectly fine PostgreSQL server already runing in Discourse container. So I want to store some of my stuff in same DB, with some fancy prefix to avoid conflict.
You could just separate the Discourse and PostgreSQL containers.
I tried to setup separated containers right from begining, but I failed hard. This setup turned out too complicated for me. Maybe because I tried to setup multisite configuration at the same time. Anyway, I don’t think this answers any of my questions.
The answer to the first question is “yes”, to the second “yes, unless you’re being sneaky”. The PostgreSQL database lives somewhere in
/var/discourse/shared/; the full path is defined in the
volumes section of your YAML config file. (If you’ve copied the
standalone sample, the full path of the database will be
This tree exists separately from the Docker containers; it sticks around even if you destroy the container and if you re-bootstrap the container the database will be unchanged (altho it may be migrated to a newer schema version if Discourse was upgraded in the meantime).
Thus, you can create databases, roles, tables etc. as you see fit and these, too, will stick around. However, if you modify the
public schema in the
discourse database, your stuff can obviously clash with later official changes and you’ll have a hard upgrading.
Finally, the backup process dumps the entire
public schema. If you’ve added a custom table, it will get dumped and restored along with Discourse’s own data just fine.
- Yes, absolutely, you can even create a new
schemato separate your data from Discourse and avoid naming conflicts, create new roles, databases, and so on.
- Yes, but only as long as you do not store your custom tables in a separate database or schema, thus risking naming conflicts when Discourse’s schema changes.
Pick your poison.
Another approach is to use domain socket in /var/discourse/shared/standalone/postgres_run/ folder:
sudo -u root psql -h /var/discourse/shared/standalone/postgres_run/
You would need to create ‘root’ user in Postgresql in container first: