# DB 所有权/清除生产数据

**URL:** <https://meta.discourse.org/t/db-ownership-wiping-out-production/113534>\
**Category:** Self-hosting\
**Created:** [2019年四月5日 11:32 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534 "2019-04-05T11:32:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 11:32 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/1 "2019-04-05T11:32:18Z")

</div>

Hi. I’m trying to wipe out my production server, and I want to know what I am doing wrong (apart from “trying to wipe out my production server” which sounds really wrong, but it’s not really in production yet, I am just working on a migration).

I am using the Discourse Docker installation, which a colleague installed for me.

This is what I tried:

```plaintext
root@discourse:/docker-app/discourse# ./launcher enter app
root@discourse-app:/var/www/discourse# su - discourse

discourse@discourse-app:~$ cd /var/www/discourse
discourse@discourse-app:/var/www/discourse$ RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake db:drop db:create db:migrate

PG::InsufficientPrivilege: ERROR: must be owner of database discourse
: DROP DATABASE IF EXISTS "discourse"
Couldn't drop database 'discourse'
rake aborted!

```

So I went to check if this ownership was really like it says and I got this:

```plaintext
discourse@discourse-app:/var/www/discourse$ psql
psql (10.5 (Ubuntu 10.5-2.pgdg16.04+1))
Type "help" for help.

discourse=> SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner"
FROM pg_catalog.pg_database d
WHERE d.datname = 'discourse'
ORDER BY 1;
   Name | Owner
-----------+----------
 discourse | postgres
(1 row)

discourse=>

```

So my questions would be…

1. Is it wrong not to have the database ownership set to `discourse` user?

2. Did I do this setup error? Or maybe my colleague. But what I mean to ask is: isn’t this something that comes with the Docker container provided?

3. How to go past this roadblock?

Thanks in advance for any help! 🙂

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2019年四月5日 11:39 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/2 "2019-04-05T11:39:55Z")

</div>

If you want to blow away the database the easiest way is to delete the postgres stuff in /var/discourse/shared/standalone and then rebuild. (from outside the container). But this is simpler:

```
 cd /var/discourse 
./launcher stop app
rm -rf shared
./launcher rebuild app

```

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 12:09 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/3 "2019-04-05T12:09:46Z")

</div>

Is there any difference in terms of what gets deleted?

I’d like to keep theme changes and settings, and delete only users, post data, etc. Does this method differ from the rake commands in terms of how much it recreates?

I don’t mind losing everything if necessary, I can redo the cosmetic changes, I am just asking.

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2019年四月5日 12:17 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/4 "2019-04-05T12:17:10Z")

</div>

> [@pgr](#):
>
> I’d like to keep theme changes and settings, and delete only users, post data, etc.

That’s not what you asked for. 😉

Themes are all in the database.

You can export the themes and reimport them on the new site. You can search here for ways to export and restore site settings.

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 12:20 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/5 "2019-04-05T12:20:47Z")

</div>

I was willing to go ahead with the `rake` commands and then find out if I still had my custom theme. 😄

But I have the necessary files to put it back in place.

Ok, so if your method is simple enough and equivalent to the other I will probably just try it.

Should I worry about my database ownership, for the future? Do I need to take any special care so it is created with the correct user as owner?

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2019年四月5日 12:25 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/6 "2019-04-05T12:25:21Z")

</div>

If you use the launcher command it does the Right Thing. If you were to use an external database then it’d be more difficult.

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 12:26 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/7 "2019-04-05T12:26:43Z")

</div>

Thank you Jay, you were very helpful as always.

---

<div class="post-metadata">

**Author:** ![JamieLanger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamielanger/32/142994_2.png) [@JamieLanger](https://meta.discourse.org/u/JamieLanger)\
**Post date:** [2019年四月5日 12:30 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/8 "2019-04-05T12:30:47Z")

</div>

1. Is it wrong not to have the database ownership set to `discourse` user?  
A. NOt exactly
2. Did I do this setup error? Or maybe my colleague. But what I mean to ask is: isn’t this something that comes with the Docker container provided?  
A. yes, this is something that comes with the Docker container provided
3. How to go past this roadblock?  
A. Delete and rebuild

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 14:19 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/9 "2019-04-05T14:19:32Z")

</div>

This image was installed months ago, in Sep 2018.

Now that I am rebuilding, should I do any updates/upgrades first? Can somebody point me to a nice tutorial for this, or at least tell what I should be searching for?

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2019年四月5日 14:23 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/10 "2019-04-05T14:23:58Z")

</div>

> [@pgr](#):
>
> Now that I am rebuilding, should I do any updates/upgrades first?

Rebuilding will take care of updating to latest 😉

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年四月5日 15:22 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/11 "2019-04-05T15:22:56Z")

</div>

I did this procedure, and the rebuild went fine (and ran all the updates as promised - 🆒!).

BUT my data is still there. Deleting `shared` didn’t delete my data. Judging by the results of `du` I think my data is in `standalone` subdir, not in `shared` subdir…

```plaintext
root@discourse:/docker-app/discourse# du -h /docker-app/ --max-depth=1
2.6M /docker-app/discourse
1.3G /docker-app/standalone
1.3G /docker-app/

```

So, can I delete `docker-app/standalone`? And did I delete anything important that I shouldn’t have in `docker-app/discourse/shared`?

P.S. - I did a snapshot of this VM before the delete, so I can go back in time if necessary

---

<div class="post-metadata">

**Author:** ![pgr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pgr/32/156783_2.png) [@pgr](https://meta.discourse.org/u/pgr)\
**Post date:** [2019年五月21日 11:39 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/12 "2019-05-21T11:39:02Z")

</div>

Just to leave some feedback here, since I had to re-do this today.

What eventually worked for me is to

- delete the full `/docker-app/standalone` directory
- no need to stop the container, the next command includes that in its script
- run `./launcher rebuild app`
- the rebuild took about 10 minutes and included an update to the code by pulling it from GitHub.

I had some trouble on my next step, so I’ll also document it here in case it’s useful for someone. I needed to **create a privileged database user** that I could use from my DB Tool of choice (which is DB Weaver on Windows).

```bash
/launcher enter app
su postgres -c "psql discourse"

```

And then:

```sql
CREATE USER myadmin WITH PASSWORD 'xxxxxxxxx';
ALTER USER myadmin WITH SUPERUSER CREATEROLE CREATEDB;
GRANT ALL PRIVILEGES ON DATABASE discourse to myadmin;

```

Use `\q` to exit that shell.

I also **created a Discourse admin** with:  
`su discourse -c 'RAILS_ENV=production RAILS_DB=discourse bundle exec rake admin:create'`

I hope this helps someone.

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2019年六月20日 11:39 UTC](https://meta.discourse.org/t/db-ownership-wiping-out-production/113534/13 "2019-06-20T11:39:05Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
