# How does one run sql queries if the container doesn't start? (rebuild failed)

**URL:** https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126
**Category:** Self-hosting
**Created:** [January 18, 2023, 8:16am UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126 "2023-01-18T08:16:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![iamntz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamntz/32/114670_2.png) [@iamntz](https://meta.discourse.org/u/iamntz)
#### Post date: [January 18, 2023, 8:16am UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/1 "2023-01-18T08:16:14Z")

</div>

Hello.

I tried to run updates & rebuild, but I ended up with this beautiful error:

```plaintext
I, [2023-01-18T08:05:48.701709 #1] INFO -- : > cd /var/www/discourse && su discourse -c 'LOAD_PLUGINS=0 bundle exec rake plugin:pull_compatible_all'
I, [2023-01-18T08:05:52.431210 #1] INFO -- :
I, [2023-01-18T08:05:52.431807 #1] INFO -- : > cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate'
2023-01-18 08:05:59.081 UTC [1166] discourse@discourse ERROR: could not create unique index "index_tags_on_name_ccnew_ccnew_ccnew5"
2023-01-18 08:05:59.081 UTC [1166] discourse@discourse DETAIL: Key (name)=(vuejs) is duplicated.
2023-01-18 08:05:59.081 UTC [1166] discourse@discourse STATEMENT: REINDEX INDEX CONCURRENTLY index_tags_on_name_ccnew_ccnew
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

PG::UniqueViolation: ERROR: could not create unique index "index_tags_on_name_ccnew_ccnew_ccnew5"
DETAIL: Key (name)=(vuejs) is duplicated.
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:110:in `exec'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:110:in `async_exec'

```

There are other topics on the forum with similar issues ([1](https://meta.discourse.org/t/cant-restore-due-to-corrupt-indexes-with-some-clues-on-how-to-deal-with-corrupt-indexes/137400/14), [2](https://meta.discourse.org/t/unable-to-restore-backup-data-unique-index-is-duplicated/147588), [3](https://meta.discourse.org/t/update-failed-due-to-primary-key-sequence-error/67852)) but when I try to enter the container I get this:

```plaintext
/var/discourse# ./launcher enter app
x86_64 arch detected.
Error: No such container: app

```

Therefore I’m unable to drop indices manually.

Any other suggestion on what aproach should I take next?

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [January 18, 2023, 8:59am UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/2 "2023-01-18T08:59:08Z")

</div>

Hello Ionut,

Thanks for the bug report. I will have a look at this, but I believe there is an issue with an index in your database and a recent migration attempts to fix that, but some issues cannot be fixed without a human intervention.

I believe the commit that introduced the migration that fails is this one:

[https://github.com/discourse/discourse/commit/8ee71d439be094f777f19a8fca44c9c2a914d479](https://github.com/discourse/discourse/commit/8ee71d439be094f777f19a8fca44c9c2a914d479)

You can try pinning your Discourse instance to the previous version which is commit `690e2f15ab9549486aaa6750e1093c1336bf17f2`. Edit your `app.yml` file and set the [`version` key under `params`](https://github.com/discourse/discourse_docker/blob/main/samples/standalone.yml#L37). Make sure to uncomment that key too!

Then, everything should start and you should be able to remove the duplicated `vuejs` tag, but that might have some unwanted effects if the duplicated tag is in use. We will try to find a better solution, but this should work until then.

---

<div class="post-metadata">

### Author: ![iamntz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamntz/32/114670_2.png) [@iamntz](https://meta.discourse.org/u/iamntz)
#### Post date: [January 18, 2023, 11:31am UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/3 "2023-01-18T11:31:32Z")

</div>

After booting up, I was able to find all duplicate tags with [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin:

```plaintext
SELECT name, count(*)
    FROM tags 
    GROUP BY name
     HAVING count(*) > 1

```

For some reasons, there were about **ten** duplicate tags. Wonder why and how those appear there _in the first place_…

Managed to wipe those sucker out, removed the version lock from `yml` file, rebuild, things looks fine now.

Thank you!

👍

---

<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 18, 2023, 10:50pm UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/4 "2023-01-18T22:50:51Z")

</div>

> [@iamntz](#):
>
> For some reasons, there were about **ten** duplicate tags. Wonder why and how those appear there _in the first place_…

We have seen this in the past, it is a nightmare problem but isolated it to being upgrade related.

Index structure in PG depends on the locale at install time and upgrades can lead to a nightmare sometimes.

[https://github.com/docker-library/postgres/issues/582](https://github.com/docker-library/postgres/issues/582)

If the reindex is not issued early enough, trouble can ensue. Some of our earlier PG docker upgrades did not run this… and some old installs sadly have a bit of a time bomb.

This is mostly resolved these days in the ecosystem but some pain remains.

---

<div class="post-metadata">

### Author: ![iamntz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamntz/32/114670_2.png) [@iamntz](https://meta.discourse.org/u/iamntz)
#### Post date: [January 19, 2023, 3:30pm UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/5 "2023-01-19T15:30:06Z")

</div>

Maybe this can help for further indentifying the cause, so here are some more details about the system:

The install is indeed ancient, since 2014, but the system is updated at least once a month (though _usually_ is every other week). So there was never a major version skipped.

There is no version lock, so basically we’re running on latest version available on `main` branch (YOLO!)

Although we’ve tried a very few non-official a while back, all installed plugins are official and it was this way for the past… 5 years or more.

---

<div class="post-metadata">

### Author: ![uwe\_keim](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/uwe_keim/32/326762_2.png) [@uwe\_keim](https://meta.discourse.org/u/uwe_keim)
#### Post date: [January 20, 2023, 4:38pm UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/6 "2023-01-20T16:38:28Z")

</div>

> [@iamntz](#):
>
> Managed to wipe those sucker out

Having the same issue, I’ve followed you until this sentence.

How do you actually remove the duplicates? Simply issuing a `delete` SQL statement in [Data Explorer](https://meta.discourse.org/t/32566?silent=true)? What about foreign tables records pointing to this tag?

#### Update 1

OK, I see that you cannot execute `update` statements.

I’ve now used the GUI and simply renamed the tag.

---

<div class="post-metadata">

### Author: ![iamntz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamntz/32/114670_2.png) [@iamntz](https://meta.discourse.org/u/iamntz)
#### Post date: [January 20, 2023, 8:29pm UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/7 "2023-01-20T20:29:43Z")

</div>

Edit the tag (by going to /tag/foo) and either rename it or remove it.

---

<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: [February 19, 2023, 8:29pm UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/8 "2023-02-19T20:29:44Z")

</div>

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