# Cannot restore database: sql key is duplicated

**URL:** https://meta.discourse.org/t/cannot-restore-database-sql-key-is-duplicated/237539
**Category:** Self-hosting
**Created:** [August 30, 2022, 5:43am UTC](https://meta.discourse.org/t/cannot-restore-database-sql-key-is-duplicated/237539 "2022-08-30T05:43:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Amethi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amethi/32/88690_2.png) [@Amethi](https://meta.discourse.org/u/Amethi)
#### Post date: [August 30, 2022, 5:43am UTC](https://meta.discourse.org/t/cannot-restore-database-sql-key-is-duplicated/237539/1 "2022-08-30T05:43:05Z")

</div>

Hi all. I’m trying to perform a server migration. I followed [this process](https://meta.discourse.org/t/restore-a-backup-from-command-line/108034).

Upon trying to restore the database via the CLI, it bombs out with the following error:

```plaintext
CREATE INDEX
ERROR: could not create unique index "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(19891, 32700, http://www.dhl-usa.com/home/home.asp) is duplicated.
EXCEPTION: psql failed: DETAIL: Key (topic_id, post_id, url)=(19891, 32700, http://www.dhl-usa.com/home/home.asp) is duplicated.
/var/www/discourse/lib/backup_restore/database_restorer.rb:92:in `restore_dump'
/var/www/discourse/lib/backup_restore/database_restorer.rb:26:in `restore'
/var/www/discourse/lib/backup_restore/restorer.rb:51:in `run'
script/discourse:149:in `restore'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
script/discourse:290:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli.rb:483:in `exec'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli.rb:31:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/cli.rb:25:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/exe/bundle:48:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/lib/bundler/friendly_errors.rb:120:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.3.18/exe/bundle:36:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Trying to rollback...
Rolling back...
Cleaning stuff up...
Dropping functions from the discourse_functions schema...
Removing tmp '/var/www/discourse/tmp/restores/default/2022-08-29-194458' directory...
Unpausing sidekiq...
Marking restore as finished...
Notifying 'system' of the end of the restore...
Finished!
[FAILED]
Restore done.

```

The forum is running fine on the old server. I put it in read-only mode, took the backup, transfered it to the new machine and started the restore.

Any ideas how to resolve this, please?

---

<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: [August 30, 2022, 10:26am UTC](https://meta.discourse.org/t/cannot-restore-database-sql-key-is-duplicated/237539/3 "2022-08-30T10:26:27Z")

</div>

It looks like you have a corrupt index. You’ll need to delete or update those entries. If you search I think there may still be some topics that describe generally hour to do it.

On the old server you try to reindex that table and keep changing stuff until it succeeds. If you’re not familiar with sql and have a budget then you can ask in #Marketplace or contact me.

What version of Discourse and postgres are you running? I haven’t seen many cases of this since postgres 13.

---

<div class="post-metadata">

### Author: ![Amethi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amethi/32/88690_2.png) [@Amethi](https://meta.discourse.org/u/Amethi)
#### Post date: [August 30, 2022, 6:22pm UTC](https://meta.discourse.org/t/cannot-restore-database-sql-key-is-duplicated/237539/4 "2022-08-30T18:22:13Z")

</div>

Thanks Jay. I’ll reach out. I’m maxed out atm.
