# Error importing backup: "could not create unique index"

**URL:** https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766
**Category:** Self-hosting
**Created:** [October 31, 2021, 8:48pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766 "2021-10-31T20:48:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![glynhudson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/glynhudson/32/120597_2.png) [@glynhudson](https://meta.discourse.org/u/glynhudson)
#### Post date: [October 31, 2021, 8:48pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/1 "2021-10-31T20:48:45Z")

</div>

Hi,

I’m attempting to migrate a forum to a new server. Both servers are running the latest version of discourse docker. When importing the backup via command line, I get the following error:

```plaintext
ERROR: could not create unique index "index_incoming_referers_on_path_and_incoming_domain_id"
DETAIL: Key (path, incoming_domain_id)=(/search/, 418) is duplicated.
EXCEPTION: psql failed: DETAIL: Key (path, incoming_domain_id)=(/search/, 418) is duplicated.

```

This seems to the same or similar error as:

> [@Restore fails - could not create unique index](https://meta.discourse.org/t/restore-fails-could-not-create-unique-index/151380/9):
>
> I have a duplicate key issue also, is there a documented fix? discourse=# REINDEX SCHEMA CONCURRENTLY public; ERROR: could not create unique index "index\_incoming\_referers\_on\_path\_and\_incoming\_domain\_id\_ccnew" DETAIL: Key (path, incoming\_domain\_id)=(/search/, 1905) is duplicated. [1] pry(main)\> IncomingReferer.where(path: "/m/search") =\> [#\<IncomingReferer:0x0000557176d3f210 id: 44231, path: "/m/search", incoming\_domain\_id: 4\>, #\<IncomingReferer:0x0000557176d925c8 id: 42228, path: "/m/…

However, in my case the duplicated records are in the `/search/` path rather than `/m/search` as is the case on the thread linked above.

I’ve connected to the container on the old server (`./launcher enter app`) and in the Rails console (`rails c`) I’ve tried to search for the duplicated records using:

`IncomingReferer.where(path: "/search")`  
and  
`IncomingReferer.where("path LIKE '%/search%'")`

However, this results in 100’s of records being displayed. How can I work out what records are duplicated, and how can I safely delete these and rebuild? The forum is currently working fine on the old server, we just need to move to new hardware.

---

<div class="post-metadata">

### Author: ![IAmGav](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iamgav/32/235598_2.png) [@IAmGav](https://meta.discourse.org/u/IAmGav)
#### Post date: [October 31, 2021, 9:07pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/2 "2021-10-31T21:07:28Z")

</div>

Have you tried using the Admin GUI ?

---

<div class="post-metadata">

### Author: ![glynhudson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/glynhudson/32/120597_2.png) [@glynhudson](https://meta.discourse.org/u/glynhudson)
#### Post date: [October 31, 2021, 9:33pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/3 "2021-10-31T21:33:45Z")

</div>

No, I assumed importing via the GUI would be invoking the same import process? I’ll try that now.

---

<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: [October 31, 2021, 11:24pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/4 "2021-10-31T23:24:56Z")

</div>

I suspect this means that you have a corrupt index. What version of Postgres are you running?

something like:

```plaintext
cd /var/discourse
cat shared/standalone/postgres_data/PG*

```

(I can’t quite remember the postgres filename).

You can search here for “postgres corrupt index” and find a topic that I once wrote about how to track down those bad records and delete them.

You basically try to rebuild that index and delete the records that it complains about then try to rebuild the index again until it rebuilds.

---

<div class="post-metadata">

### Author: ![glynhudson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/glynhudson/32/120597_2.png) [@glynhudson](https://meta.discourse.org/u/glynhudson)
#### Post date: [October 31, 2021, 11:33pm UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/5 "2021-10-31T23:33:57Z")

</div>

Just tried importing via the GUI, with exactly the same result:

 ![Screenshot 2021-10-31 23.26.14](https://global.discourse-cdn.com/meta/original/3X/6/f/6f39df205e418700e0d5fc9747451abcb78b244e.png)

> [@pfaffman](#):
>
> What version of Postgres are you running?

The old server doesn’t have a file called `PG_VERSION`, how can I tell what version its running? I’ve updated the docket install to the latest version today.

The new server (newly bootstrapped) is running postgres V13

```plaintext
cat shared/standalone/postgres_data/PG_VERSION
13

```

> [@pfaffman](#):
>
> You can search here for “postgres corrupt index” and find a topic that I once wrote about how to track down those bad records and delete them.
> 
> You basically try to rebuild that index and delete the records that it complains about then try to rebuild the index again until it rebuilds.

Is there a recommended procedure on how to do this?

---

<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: [November 1, 2021, 12:33am UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/6 "2021-11-01T00:33:44Z")

</div>

I had a topic that had some hints, but I don’t see it anymore. It’s been close to a year since the postgres 12 upgrade.

```
  reindex index index_incoming_referers_on_path_and_incoming_domain_id;

```

And

```
 ActiveRecord::Base.connection.execute('reindex index index_incoming_referers_on_path_and_incoming_domain_id;')

```

Are ways to try to rebuild the index. It’ll give you an error and you can then go and delete the errant records. You’ll need to include both the path and the ID.

---

<div class="post-metadata">

### Author: ![glynhudson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/glynhudson/32/120597_2.png) [@glynhudson](https://meta.discourse.org/u/glynhudson)
#### Post date: [November 1, 2021, 2:12am UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/7 "2021-11-01T02:12:14Z")

</div>

Ok, I’ve fixed it. I did the following

Enter container

`./launcher enter app`

Connect to database

`su postgres -c 'psql discourse'`

Try to find duplicates

```plaintext
discourse=# select * from incoming_referers where path LIKE '%/search/' ORDER BY incoming_domain_id;`

  id | path | incoming_domain_id
------+------------+--------------------
 3339 | /search/ | 33
 6257 | /search/ | 91
 1567 | /search/ | 298
 1777 | /search/ | 341
 3010 | /search/ | 418
 6247 | /search/ | 418
 4293 | /search/ | 644
 2899 | /search/ | 653
 3447 | /search/ | 793
 3696 | /search/ | 852
 4395 | /a/search/ | 1050
 6968 | /search/ | 1305
 5634 | /search/ | 1387
 5834 | /search/ | 1437
 6519 | /search/ | 1637
 7127 | /search/ | 1787
 7280 | /search/ | 1827
(17 rows)

```

Delete duplicate

`DELETE FROM incoming_referers WHERE path LIKE '%/search/' AND id IN (6247);`

Then rebuild

```plaintext
discourse=# REINDEX SCHEMA CONCURRENTLY public;
WARNING: cannot reindex invalid index "public.incoming_referers_pkey_ccnew" concurrently, skipping
WARNING: cannot reindex invalid index "public.index_incoming_referers_on_path_and_incoming_domain_id_ccnew" concurrently, skipping
WARNING: cannot reindex invalid index "pg_toast.pg_toast_20732_index_ccnew" concurrently, skipping
REINDEX

```

Then I took another backup, copied it to the new server, and it imported successfully 😃

It would be nice if the backup process could spot duplicates to avoid any issues, I was luckily that I had access to the original server which was still running. If I was restoring a cold backup, this would probably have been more of an issue\>

Thanks a lot for your help.

---

<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: [December 1, 2021, 2:12am UTC](https://meta.discourse.org/t/error-importing-backup-could-not-create-unique-index/207766/8 "2021-12-01T02:12:40Z")

</div>

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