Unique violation: 7 ERROR: duplicate key value violates unique constraint "oc_preferences_pkey"

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    • 32.0.3
  • Operating system and version (e.g., Ubuntu 24.04):
    • Ubuntu 22.04.5 LTS x86_64
  • Is this the first time you’ve seen this error? (Yes / No):
    • No
  • When did this problem seem to first start?
    • today after update
  • Installation method
    • docker compose

Summary of the issue you are facing:

I update Nextcloud to 32.0.3 version from 30 version. After update I successful login in admin. But now I cant login in admin. I have 500 error Internal Server Error. In nextcloud.log I see this error

“An exception occurred while executing a query: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint “oc_preferences_pkey”\nDETAIL: Key (userid, appid, configkey)=(admin, login, lastLogin) already exists.”

At the same time, I can easily log in as my user

Log entries

Nextcloud

"reqId":"kq7aKmaa11SJtEvGjKRc","level":3,"time":"2025-12-25T18:37:19+00:00","remoteAddr":"172.21.0.1","user":"admin","app":"index","method":"POST","url":"/login","message":"An exception occurred while executing a query: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint \"oc_preferences_pkey\"\nDETAIL:  Key (userid, appid, configkey)=(admin, login, lastLogin) already exists.","userAgent":"Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0","version":"32.0.3.2"

Configuration

Nextcloud

{
    "system": {
        "htaccess.RewriteBase": "\/",
        "memcache.local": "\\OC\\Memcache\\APCu",
        "apps_paths": [
            {
                "path": "\/var\/www\/html\/apps",
                "url": "\/apps",
                "writable": false
            },
            {
                "path": "\/var\/www\/html\/custom_apps",
                "url": "\/custom_apps",
                "writable": true
            }
        ],
        "memcache.distributed": "\\OC\\Memcache\\Redis",
        "memcache.locking": "\\OC\\Memcache\\Redis",
        "redis": {
            "host": "***REMOVED SENSITIVE VALUE***",
            "password": "***REMOVED SENSITIVE VALUE***",
            "port": 6379
        },
        "upgrade.disable-web": true,
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "trusted_domains": [
            "my.domain"
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "dbtype": "pgsql",
        "version": "32.0.3.2",
        "overwrite.cli.url": "https:\/\/my.domain",
        "overwriteprotocol": "https",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "",
        "dbtableprefix": "oc_",
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "installed": true,
        "auth.bruteforce.protection.enabled": true,
        "memories.db.triggers.fcu": true,
        "memories.exiftool": "\/var\/www\/html\/custom_apps\/memories\/bin-ext\/exiftool-amd64-glibc",
        "memories.vod.path": "\/var\/www\/html\/custom_apps\/memories\/bin-ext\/go-vod-amd64",
        "loglevel": 2,
        "log_type": "file",
        "logfile": "nextcloud.log",
        "maintenance": false,
        "mail_smtpmode": "smtp",
        "mail_smtpsecure": "ssl",
        "mail_sendmailmode": "smtp",
        "mail_from_address": "***REMOVED SENSITIVE VALUE***",
        "mail_domain": "***REMOVED SENSITIVE VALUE***"
    }
}

Apps

Enabled:

activity: 5.0.0-dev.0
app_api: 32.0.0
bruteforcesettings: 5.0.0-dev.0
circles: 32.0.0
cloud_federation_api: 1.16.0
comments: 1.22.0
contactsinteraction: 1.13.1
dashboard: 7.12.0
dav: 1.34.2
federatedfilesharing: 1.22.0
federation: 1.22.0
files: 2.4.0
files_downloadlimit: 5.0.0-dev.0
files_pdfviewer: 5.0.0-dev.0
files_reminders: 1.5.0
files_sharing: 1.24.1
files_trashbin: 1.22.0
files_versions: 1.25.0
firstrunwizard: 5.0.0-dev.0
logreader: 5.0.0-dev.0
lookup_server_connector: 1.20.0
memories: 7.7.0
nextcloud_announcements: 4.0.0-dev.0
notifications: 5.0.0-dev.0
oauth2: 1.20.0
password_policy: 4.0.0-dev.0
photos: 5.0.0-dev.1
privacy: 4.0.0-dev.0
profile: 1.1.0
provisioning_api: 1.22.0
recommendations: 5.0.0-dev.0
related_resources: 3.0.0-dev.0
serverinfo: 4.0.0-dev.0
settings: 1.15.1
sharebymail: 1.22.0
support: 4.0.0-dev.0
survey_client: 4.0.0-dev.0
systemtags: 1.22.0
text: 6.0.1
theming: 2.7.0
twofactor_backupcodes: 1.21.0
updatenotification: 1.22.0
user_status: 1.12.0
viewer: 5.0.0-dev.0
weather_status: 1.12.0
webhook_listeners: 1.3.0
workflowengine: 2.14.0

Disabled:

admin_audit: 1.22.0
encryption: 2.20.0
files_external: 1.24.0
mediadc: 0.4.0 (installed 0.4.0)
suspicious_login: 10.0.0-dev.0
twofactor_nextcloud_notification: 6.0.0-dev.0
twofactor_totp: 14.0.0
user_ldap: 1.23.0

Looks like we managed to solve the problem. That’s what I did:

  1. Connect to docker container sudo docker exec --user www-data -it nextcloud-db-1 bash
  2. Connect to postgresql psql -U nextcloud -d nextcloud
  3. Checked the entries in the table:
SELECT * FROM oc_preferences
WHERE userid = 'admin'
  AND appid   = 'login'
  AND configkey = 'lastLogin';
  1. Delete duplicate row:
DELETE FROM oc_preferences
WHERE ctid IN (
    SELECT ctid
    FROM   oc_preferences
    WHERE  userid    = 'admin'
      AND  appid     = 'login'
      AND  configkey = 'lastLogin'
    LIMIT 1
);

And now error disappeared

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