How to access the discourse database?

Hi all,

This is probably a stupid question but how can I access the database?
Is there something equivalent to phpadmin (or another GUI) that I can use to view and edit the database of discourse? If not, then what is the best way to access/view/edit it?

3 个赞

Install the Data Explorer Plugin and have at it.

If you want to do postgres command line stuff, you’ll need to enter the container to do that using the Docker enter command.

5 个赞

Hi, Docker enter, but what’s next?

I tried psql but got psql: FATAL: role "root" does not exist

1 个赞

postgres runs under the postgres user, per:

You want to sudo postgres psql discourse to hit the right user and database…

4 个赞

I see , thank you! @sam

In case anyone else is confused the actual command you need to run inside the container is:

sudo -u postgres psql discourse

i.e. ‘Run psql discourse as the postgres user.’

25 个赞

sudo -u postgres psql discourse

This lists the 156 tables:

discourse=# \dt

But basic queries return nothing:

discourse=# select * from users limit 1
discourse-#

This is a working site, so I’m sure there is data. What am I doing wrong?

1 个赞

与上面的 Mark 遇到同样的问题

  1. 如可能,请使用 data-explorer 插件——它附带一些额外的安全检查和一个模式浏览器。
  2. 在 psql 控制台中,您需要用 ; 分号来结束命令。
8 个赞

你能解释一下这个过程吗?我是容器领域的新手,很想开始玩弄我的 PostgreSQL 数据库,而不是让容器对我成为一个巨大的黑盒 :slight_smile:

1 个赞

这就为您奉上,专门给 @Divert 的:

假设您处于标准支持配置下的开箱即用(OOTB)单容器模式:

cd /var/discourse
./launcher enter app
su discourse
psql discourse

如果一切运行正常,您现在已作为 discourse 用户登录到 discourse 数据库,可以执行 PostgreSQL 和 SQL 命令了。

接下来,如果您想列出数据库中的所有表,可以输入:

\dt

如果您想列出所有包含 “setting” 这个词的表,可以输入:

\dt *setting* 

如果您找到了感兴趣的表,比如 users 表,可以这样查看表结构:

\d users

如果您想(例如,虽然这并不是一个高效的示例)查看前 10 个用户(按 id 降序排列,仅取 10 条),只需输入:

SELECT * FROM users ORDER BY id desc LIMIT 10;

总之,您应该明白意思了。如果您精通 SQL,这简直易如反掌。

这里的每个人都会建议您在测试环境(staging setup)上尝试(因为如果弄坏了,只需重建并重新开始),尽情玩耍吧!

在您对技术非常熟悉(至少掌握了基础知识)之前,请不要在拥有真实用户的生产系统上随意尝试,并且务必先进行完整备份 :slight_smile:

祝玩得开心 @Divert


注意:这种在数据库中的“随意尝试”或“自学”并不是 meta 论坛 support 板块的主题,所以基本上您得靠自己了。祝玩得愉快!

18 个赞

@neounix 非常感谢您的回答,您真的付出了额外的努力,我对此深表感激。:smile:

6 个赞

有没有办法通过某种 SQL 客户端(例如 Mac 上的 PSequel)连接到 Discourse 数据库(Docker 自托管安装)?

除了使用我不熟悉的 Docker(我不想损坏数据库)之外,是否有可以通过 Data Explorer 插件运行的 SQL 命令来显示我可以在 Discourse 中访问的数据库的名称?