Skip to main content

Foreman - Upgrade fails on step foreman-rake db:migrate with 'PG::InsufficientPrivilege: ERROR: must be owner of extension evr'

While upgrading my Foreman+Katello Server to the latest version, I've encountered the following issue:

 [archy@katello01 ~]$ sudo foreman-installer --scenario katello  
 2025-04-20 11:25:08 [NOTICE] [root] Loading installer configuration. This will take some time.  
 2025-04-20 11:25:12 [NOTICE] [root] Running installer with log based terminal output at level NOTICE.  
 2025-04-20 11:25:12 [NOTICE] [root] Use -l to set the terminal output log level to ERROR, WARN, NOTICE, INFO, or DEBUG. See --full-help for definitions.  
 2025-04-20 11:25:14 [NOTICE] [checks] System checks passed  
 2025-04-20 11:25:21 [NOTICE] [pre] The Foreman database foreman does not exist.  
 2025-04-20 11:25:21 [NOTICE] [configure] Starting system configuration.  
 2025-04-20 11:25:31 [NOTICE] [configure] 250 configuration steps out of 1939 steps complete.  
 2025-04-20 11:25:34 [NOTICE] [configure] 500 configuration steps out of 1940 steps complete.  
 2025-04-20 11:25:42 [NOTICE] [configure] 1000 configuration steps out of 1950 steps complete.  
 2025-04-20 11:25:42 [NOTICE] [configure] 1250 configuration steps out of 1952 steps complete.  
 2025-04-20 11:25:43 [NOTICE] [configure] 1500 configuration steps out of 2111 steps complete.  
 2025-04-20 11:25:43 [NOTICE] [configure] 1750 configuration steps out of 2156 steps complete.  
 2025-04-20 11:26:09 [ERROR ] [configure] '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]  
 2025-04-20 11:26:09 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: change from 'notrun' to ['0'] failed: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of  
 [0]  
 2025-04-20 11:26:31 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]: Failed to call refresh: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]  
 2025-04-20 11:26:31 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]  
 2025-04-20 11:27:08 [NOTICE] [configure] 2000 configuration steps out of 2156 steps complete.  
 2025-04-20 11:27:27 [NOTICE] [configure] System configuration has finished.  
 Error 1: Puppet Exec resource 'foreman-rake-db:migrate' failed. Logs:  
  /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]  
   Adding autorequire relationship with User[foreman]  
   Starting to evaluate the resource (1872 of 2156)  
   Failed to call refresh: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]  
   '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]  
   Evaluated in 45.77 seconds  
  Exec[foreman-rake-db:migrate](provider=posix)  
   Executing check '/usr/sbin/foreman-rake db:abort_if_pending_migrations'  
   Executing '/usr/sbin/foreman-rake db:migrate'  
   Executing check '/usr/sbin/foreman-rake db:abort_if_pending_migrations'  
   Executing '/usr/sbin/foreman-rake db:migrate'  
  /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/unless  
   Run `bin/rails db:migrate` to update your database then try again.  
   You have 10 pending migrations:  
    20240312133027 ExtendTemplateInvocationEvents  
    20240924161240 KatelloRecreateEvrConstructs  
    20241022121706 AddSyncDependenciesOption  
    20241101144625 RemoveSystemPurposeAddons  
    20241107002541 AddRegistryURLToKatelloFlatpakRemotes  
    20241112145802 AddManifestEntityToContentFacets  
    20241120213713 AddAllowOtherTypesToContentViewErratumFilterRules  
    20241126150849 RemoveRemoteExecutionWorkersPoolSize  
    20241206183052 AddContentTypeToContainerManifestsAndLists  
    20250309121956 RenameAnsibleTowerFqdnToApiURL  
   Run `bin/rails db:migrate` to update your database then try again.  
   You have 9 pending migrations:  
    20240924161240 KatelloRecreateEvrConstructs  
    20241022121706 AddSyncDependenciesOption  
    20241101144625 RemoveSystemPurposeAddons  
    20241107002541 AddRegistryURLToKatelloFlatpakRemotes  
    20241112145802 AddManifestEntityToContentFacets  
    20241120213713 AddAllowOtherTypesToContentViewErratumFilterRules  
    20241126150849 RemoveRemoteExecutionWorkersPoolSize  
    20241206183052 AddContentTypeToContainerManifestsAndLists  
    20250309121956 RenameAnsibleTowerFqdnToApiURL  
  /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns  
   rake aborted!  
   StandardError: An error has occurred, this and all later migrations canceled:  
     PG::InsufficientPrivilege: ERROR: must be owner of extension evr  
When running 'foreman-rake db:migrate' manually, I get the exact step it's failing on:
 [archy@katello01 ~]$ sudo foreman-rake db:migrate  
 load average: 0.26 0.84 0.74  
 == 20240924161240 KatelloRecreateEvrConstructs: migrating =====================  
 -- extension_enabled?("evr")  
   -> 0.0034s  
 -- execute("DROP EXTENSION evr CASCADE;\n")  
 rake aborted!  
 StandardError: An error has occurred, this and all later migrations canceled:  
 PG::InsufficientPrivilege: ERROR: must be owner of extension evr  
Now with that figured out, we can start to fix the issue. This is usually a one-off Issue and can be fixed by dropping the extension 'evr' in postgresql. Switch to the 'postgres' user and connect to the 'foreman' database:
 [archy@katello01 ~]$ sudo -Hiu postgres  
 [postgres@katello01 ~]$ psql  
 postgres=# \c foreman  
Now remove the extension:
 foreman=# DROP EXTENSION evr CASCADE;  
Output:
 NOTICE: drop cascades to 6 other objects  
 DETAIL: drop cascades to trigger evr_insert_trigger_katello_installed_packages on table katello_installed_packages  
 drop cascades to trigger evr_insert_trigger_katello_rpms on table katello_rpms  
 drop cascades to trigger evr_update_trigger_katello_installed_packages on table katello_installed_packages  
 drop cascades to trigger evr_update_trigger_katello_rpms on table katello_rpms  
 drop cascades to column evr of table katello_rpms  
 drop cascades to column evr of table katello_installed_packages  
 DROP EXTENSION  
Stop the currently running services and rerun the installer to ensure consistency:
 [archy@katello01 ~]$ sudo foreman-maintain service stop  
 [archy@katello01 ~]$ sudo foreman-installer --scenario katello  
The installation should now run through and foreman should be up and running as expected by the end of it. However, this will leave Errata to not be functional.
In order to activate Errata Generation again, we need to recreate the 'evr' extension again:
 [archy@katello01 ~]$ sudo -Hiu postgres   
 [postgres@katello01 ~]$ psql   
 postgres=# \c foreman   
 foreman=# CREATE EXTENSION IF NOT EXISTS "evr" CASCADE;  
Set the owner appropriately:
 [archy@katello01 ~]$ sudo runuser -l postgres -c "psql -d foreman -c \"UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE rolname='foreman') WHERE extname='evr';\""  
The extension should now be available and the db migration can be run again:
 [archy@katello01 ~]$ sudo foreman-rake db:migrate --trace  
After the foreman-rake task completed successfully, search for the file responsible for the db migration included during the upgrade:
 [archy@katello01 ~]$ sudo file /usr/share/gems/gems/katello-4.16.*/db/migrate/20240924161240_katello_recreate_evr_constructs.rb  
Run the migration again manually to make sure it executed correctly:
 [archy@katello01 ~]$ sudo foreman-rake console  
 irb(main):001:0> require '/usr/share/gems/gems/katello-4.16.1/db/migrate/20240924161240_katello_recreate_evr_constructs.rb'  
 irb(main):001:0> krec = KatelloRecreateEvrConstructs.new  
 irb(main):001:0> krec.extension_enabled?("evr")  
 irb(main):001:0> krec.up  
 irb(main):001:0> exit()  
The migration tasks have all run now and we can run the installer one final time:
 [archy@katello01 ~]$ sudo foreman-maintain service stop  
 [archy@katello01 ~]$ sudo foreman-installer --scenario katello  
Foreman will be available again after the successfull foreman-installer run including working errata generation.

Feel free to comment and / or suggest a topic.

Comments