# How to access the discourse database?

**URL:** https://meta.discourse.org/t/how-to-access-the-discourse-database/41845
**Category:** Self-hosting
**Created:** [March 31, 2016, 8:57pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845 "2016-03-31T20:57:21Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Newuser](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/newuser/32/184725_2.png) [@Newuser](https://meta.discourse.org/u/Newuser)
#### Post date: [March 31, 2016, 8:57pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/1 "2016-03-31T20:57:21Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [March 31, 2016, 9:09pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/2 "2016-03-31T21:09:29Z")

</div>

Install the [Data Explorer Plugin](https://github.com/discourse/discourse-data-explorer) 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.

---

<div class="post-metadata">

### Author: ![FrankFang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frankfang/32/120473_2.png) [@FrankFang](https://meta.discourse.org/u/FrankFang)
#### Post date: [January 12, 2017, 12:00pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/4 "2017-01-12T12:00:07Z")

</div>

Hi, Docker enter, but what’s next?

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

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 17, 2017, 2:59pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/5 "2017-01-17T14:59:02Z")

</div>

> [@FrankFang](#):
>
> I tried psql but got psql: FATAL: role “root” does not exist

postgres runs under the postgres user, per:

> <https://github.com/discourse/discourse_docker/blob/master/templates/postgres.template.yml#L40>

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

---

<div class="post-metadata">

### Author: ![FrankFang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frankfang/32/120473_2.png) [@FrankFang](https://meta.discourse.org/u/FrankFang)
#### Post date: [January 17, 2017, 3:30pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/6 "2017-01-17T15:30:10Z")

</div>

I see , thank you! @sam

---

<div class="post-metadata">

### Author: ![jacktonkin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacktonkin/32/121110_2.png) [@jacktonkin](https://meta.discourse.org/u/jacktonkin)
#### Post date: [April 3, 2017, 1:45pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/7 "2017-04-03T13:45:32Z")

</div>

> [@sam](#):
>
> sudo postgres psql discourse

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.’

---

<div class="post-metadata">

### Author: ![Mark\_Schmucker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mark_schmucker/32/124810_2.png) [@Mark\_Schmucker](https://meta.discourse.org/u/Mark_Schmucker)
#### Post date: [December 8, 2019, 12:50am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/10 "2019-12-08T00:50:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jennifer\_Abrams](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jennifer_abrams/32/150388_2.png) [@Jennifer\_Abrams](https://meta.discourse.org/u/Jennifer_Abrams)
#### Post date: [May 19, 2020, 5:55am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/11 "2020-05-19T05:55:29Z")

</div>

same problem as mark above

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [May 19, 2020, 6:35am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/12 "2020-05-19T06:35:47Z")

</div>

1. Use the data-explorer plugin if at all possible - it comes with some bonus safety checks and a schema explorer.
2. You need to terminate commands in the psql console with a ; semicolon.

---

<div class="post-metadata">

### Author: ![Divert](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@Divert](https://meta.discourse.org/u/Divert)
#### Post date: [June 4, 2020, 4:36am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/13 "2020-06-04T04:36:04Z")

</div>

> [@codinghorror](#):
>
> using the Docker `enter` command.

Can you please explain this process, I am a newbie on containers and would love to start playing with my postgres DB instead of having the container be a big black box to me 🙂

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [June 4, 2020, 7:15am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/14 "2020-06-04T07:15:58Z")

</div>

> [@Divert](#):
>
> Can you please explain this process, I am a newbie on containers and would love to start playing with my postgres DB instead of having the container be a big black box to me 🙂

Here ya go, just for you @Divert :

Assuming you are in OOTB single container mode in the standard supported setup:

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

```

Now, if all was running properly, you are in the DB as the discourse user connected to the discourse DB ready to issue PostgreSQL and SQL commands.

Then, if you want to list all the tables in the DB, you can:

```plaintext
\dt

```

If you want to list all tables with the word “setting”, then you can:

```plaintext
\dt *setting* 

```

If you find a table of interest, say the users table, you can examine the table structure, like so:

```plaintext
\d users

```

If you wish (for example, and not an efficient example at that) to examine the first 10 users (ordered by id descending, only 10); you can just:

```plaintext
SELECT * FROM users ORDER BY id desc LIMIT 10;

```

Anyway, you get the idea. If you are good at SQL, it’s a piece-of-cake.

Everyone here will recommend you play around on a staging setup (where if you break things you can just rebuild and start over) and have fun!

Don’t play around on a production system with real users, until you are very comfortable with the tech (maybe have mastered the basics at least) and always make full backups first 🙂

Have fun @Divert

* * *

Note: This kind of “playing around” or “self learning” in the DB is not the topic of `support` at meta, so. you are basically on your own. Enjoy!

---

<div class="post-metadata">

### Author: ![Divert](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@Divert](https://meta.discourse.org/u/Divert)
#### Post date: [June 6, 2020, 4:11am UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/15 "2020-06-06T04:11:55Z")

</div>

@neounix Thank you so much for this answer, you did go the extra mile and I really appreciate it. 😄

---

<div class="post-metadata">

### Author: ![profcaju](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/profcaju/32/301565_2.png) [@profcaju](https://meta.discourse.org/u/profcaju)
#### Post date: [October 12, 2020, 8:02pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/16 "2020-10-12T20:02:53Z")

</div>

Is there some way to connect to Discourse DB (docker self-hosted install) using some SQL Client (like PSequel for mac)?

---

<div class="post-metadata">

### Author: ![DScryber](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dscryber/32/220457_2.png) [@DScryber](https://meta.discourse.org/u/DScryber)
#### Post date: [March 10, 2022, 8:26pm UTC](https://meta.discourse.org/t/how-to-access-the-discourse-database/41845/17 "2022-03-10T20:26:03Z")

</div>

Aside from using Docker, which I’m unfamiliar with (and don’t want to corrupt the DB) are there SQL commands I could run through the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin to show the names of the databases I can access in Discourse?
