# Error running remap after changing site url

**URL:** https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649
**Category:** Self-hosting
**Tags:** sql-query, domains, postgres
**Created:** [July 2, 2026, 1:59pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649 "2026-07-02T13:59:51Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 1:59pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/1 "2026-07-02T13:59:52Z")

</div>

I changed the URL of my discourse site, and am following the directions in [Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-your-discourse/16098). When attempting to run remap, I am repeatedly getting the following error. It keeps telling me to re-run the script but then the same error occurs each time.[[1]](#footnote-2003495-1)

I’m not sure what my next move is here and would be grateful for guidance. Thanks in advance! :seedling:

```bash
root@digitallysovereign:/var/discourse# ./launcher enter app
x86_64 arch detected.
root@digitallysovereign-app:/var/www/discourse# discourse remap discourse.tobiaseigen.org digitallysovereign.org
Rewriting all occurrences of discourse.tobiaseigen.org to digitallysovereign.org
WILL RUN ON 'default' DB
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES): YES

Remapping tables on default...

ai_api_audit_logs=919
ai_secrets=1
backup_metadata=1
browser_pageview_events=3664
Error: ERROR: duplicate key value violates unique constraint "idx_bprd_rollups_date_referrer_unique"
DETAIL: Key (date, normalized_referrer)=(2026-07-01, digitallysovereign.org) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.
root@digitallysovereign-app:/var/www/discourse# discourse remap discourse.tobiaseigen.org digitallysovereign.org
Rewriting all occurrences of discourse.tobiaseigen.org to digitallysovereign.org
WILL RUN ON 'default' DB
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES): YES

Remapping tables on default...

Error: ERROR: duplicate key value violates unique constraint "idx_bprd_rollups_date_referrer_unique"
DETAIL: Key (date, normalized_referrer)=(2026-07-01, digitallysovereign.org) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.
root@digitallysovereign-app:/var/www/discourse# discourse remap discourse.tobiaseigen.org digitallysovereign.org
Rewriting all occurrences of discourse.tobiaseigen.org to digitallysovereign.org
WILL RUN ON 'default' DB
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES): YES

Remapping tables on default...

Error: ERROR: duplicate key value violates unique constraint "idx_bprd_rollups_date_referrer_unique"
DETAIL: Key (date, normalized_referrer)=(2026-07-01, digitallysovereign.org) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

* * *

1. I know the definition of insanity is to repeat the same thing over and over again and to expect a different result! :rofl: [↩︎](#footnote-ref-2003495-1)

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 2, 2026, 4:09pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/2 "2026-07-02T16:09:13Z")

</div>

looks like a constraint collision in the postgres analytics table. your database already includes records of the new domain on specific dates so the remap tool is likely creating duplicates and postgres is rejecting them.

i would try to delete the old domain records in the specific table only for the dates where the new domain already has data in order to preserve historical data and unblock the remap tool. do a safety backup first though.

try this:

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

```

```plaintext
# create a safety backup
discourse backup

```

```plaintext
# enter the database console
sudo -u postgres psql discourse

```

```sql
/* find the exact table name tied to this index */
SELECT tablename 
FROM pg_indexes 
WHERE indexname = 'idx_bprd_rollups_date_referrer_unique';

```

assuming the query above returns `browser_pageview_rollup_details` then use that table name in the next query

```sql
/* delete the colliding analytics records */
DELETE FROM browser_pageview_rollup_details 
WHERE normalized_referrer = 'discourse.tobiaseigen.org' 
AND date IN (
    SELECT date 
    FROM browser_pageview_rollup_details 
    WHERE normalized_referrer = 'digitallysovereign.org'
);

/* exit postgres */
\q

```

then run the remap tool again

```plaintext
discourse remap discourse.tobiaseigen.org digitallysovereign.org

```

then use `rebake_match` rake task instead of full rebake

```plaintext
# rebake only posts containing the new domain string
rake posts:rebake_match["digitallysovereign.org"]

```

---

<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: [July 2, 2026, 4:52pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/3 "2026-07-02T16:52:23Z")

</div>

I think Lilly’s idea should work.

What I do is backup the database, change the URL, and then restore and let the Discourse remapper do the job. That code gets used by Discourse hosting and if it fails someone who can fix it will notice. :slight_smile:

But yeah, those referrer links have caused all kinds of issues.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 5:24pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/4 "2026-07-02T17:24:44Z")

</div>

Thanks, Lilly!

`discourse db` doesn’t work for me - I am using `sudo -u postgres psql discourse`.

The table returned by the first query was actually `browser_pageview_referrer_daily_rollups` so I used that in the second query.

Now I am getting a different error. When I run that second query again it does not delete anything.

```plaintext
Error: ERROR: duplicate key value violates unique constraint "idx_bprd_rollups_date_referrer_unique"
DETAIL: Key (date, normalized_referrer)=(2026-06-30, digitallysovereign.org/c/members/32) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 2, 2026, 6:04pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/5 "2026-07-02T18:04:33Z")

</div>

ok i guess the initial query used exact matching and it missed all the rows with paths attached. i would try this sql query i think (i can’t really test any of this because i have no need to change a domain name on my nameserver).

```sql
DELETE FROM browser_pageview_referrer_daily_rollups old_table
WHERE old_table.normalized_referrer LIKE '%discourse.tobiaseigen.org%'
AND EXISTS (
    SELECT 1 
    FROM browser_pageview_referrer_daily_rollups new_table
    WHERE new_table.date = old_table.date
    AND new_table.normalized_referrer = REPLACE(old_table.normalized_referrer, 'discourse.tobiaseigen.org', 'digitallysovereign.org')
);

```

```plaintext
\q

```

if that works without error then run the remap tool and rebake rake task i posted above.

i did do a forum migration to a new server and new domain name a few months ago, but the remap tool worked perfectly for me the first time.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 6:51pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/6 "2026-07-02T18:51:02Z")

</div>

Thanks again, Lilly! You’re a star.

I am inching my way through the database, it seems. 24 records were deleted! Now I am at `unique_post_links`.

```bash
root@digitallysovereign-app:/var/www/discourse# discourse remap discourse.tobiaseigen.org digitallysovereign.org
Rewriting all occurrences of discourse.tobiaseigen.org to digitallysovereign.org
WILL RUN ON 'default' DB
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES): yes

Remapping tables on default...

browser_pageview_referrer_daily_rollups=1466
categories=2
chat_message_links=4
chat_message_search_data=4
chat_messages=5
discourse_activity_pub_collections=1
discourse_activity_pub_objects=1
drafts=4
email_logs=797
group_histories=2
groups=2
incoming_emails=21
moved_posts=2
notifications=10
post_localizations=51
post_revisions=31
post_search_data=226
posts=774
site_settings=1
stylesheet_cache=1168
Error: ERROR: duplicate key value violates unique constraint "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(581, 3696, https://digitallysovereign.org) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 2, 2026, 7:03pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/7 "2026-07-02T19:03:26Z")

</div>

ok try the same logic but let’s verify first:

```sql
SELECT tablename 
FROM pg_indexes 
WHERE indexname = 'unique_post_links';

```

should return `topic_links`? thus this is the next logical step:

```sql
DELETE FROM topic_links old_link
WHERE old_link.url LIKE '%discourse.tobiaseigen.org%'
AND EXISTS (
    SELECT 1 
    FROM topic_links new_link
    WHERE new_link.topic_id = old_link.topic_id
    AND new_link.post_id = old_link.post_id
    AND new_link.url = REPLACE(old_link.url, 'discourse.tobiaseigen.org', 'digitallysovereign.org')
);

```

```sql
\q

```

i think this is near the end of the database schema so it should likely be the final hurdle (i hope) before running the remap and rake task i posted above…

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 7:10pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/8 "2026-07-02T19:10:01Z")

</div>

All done! :rocket:

```plaintext
root@digitallysovereign-app:/var/www/discourse# discourse remap discourse.tobiaseigen.org digitallysovereign.org
Rewriting all occurrences of discourse.tobiaseigen.org to digitallysovereign.org
WILL RUN ON 'default' DB
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES): yes

Remapping tables on default...

topic_links=1449
topic_search_data=11
topics=11
user_auth_token_logs=5
user_histories=131
Done

```

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 2, 2026, 7:11pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/9 "2026-07-02T19:11:59Z")

</div>

i guess you should mark one of these as a solution :wink:

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 7:16pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/10 "2026-07-02T19:16:14Z")

</div>

Will do!

Can you explain to me what `discourse db` is and why you can do that and I can’t? Seems easier to remember than `sudo -u postgres psql discourse`!

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 2, 2026, 7:21pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/11 "2026-07-02T19:21:25Z")

</div>

i was just misremembering because it’s rare that i do this sort of sql in the container. i did think that was the correct command. i think it’s because Discourse has built in commands like `discourse backup` and `discourse remap`.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [July 2, 2026, 7:27pm UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/12 "2026-07-02T19:27:42Z")

</div>

That’s a feature request in disguise!

:disguised_face:

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [July 3, 2026, 2:20am UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/13 "2026-07-03T02:20:41Z")

</div>

yea the reason that this command cannot be added is it would have to vary according to the database architecture. i tried a PR but the actual command won’t work because it is different for say, [standard install](https://meta.discourse.org/t/142537?silent=true) vs dual container and multisite. the script lives inside the discourse core container, so even if the admin enters the correct database container (in non-[standard install](https://meta.discourse.org/t/142537?silent=true)), it will fail because the script where the command would be stored (`script/discourse`) is executed outside of it in say `web_only.yml` for a dual container build. basically the only way this command could work is for a [standard install](https://meta.discourse.org/t/142537?silent=true) where the app and database are in the same container.

---

<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: [August 2, 2026, 2:21am UTC](https://meta.discourse.org/t/error-running-remap-after-changing-site-url/406649/14 "2026-08-02T02:21:27Z")

</div>

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