{"id":1213,"date":"2025-07-25T13:34:05","date_gmt":"2025-07-25T11:34:05","guid":{"rendered":"https:\/\/sven-seeberg.de\/wp\/?p=1213"},"modified":"2025-07-29T15:56:33","modified_gmt":"2025-07-29T13:56:33","slug":"migrating-forgejo-from-sqlite-to-postgresql","status":"publish","type":"post","link":"https:\/\/sven-seeberg.de\/wp\/?p=1213","title":{"rendered":"Migrating Forgejo from SQLite to PostgreSQL"},"content":{"rendered":"\n<p>The easiest path for migrating Forgejo to PostgreSQL seems to be with the <code><a href=\"https:\/\/erik.thauvin.net\/blog\/posts\/9766\/migrate-gitea-forgejo-from-mariadb-mysql-to-postgresql\" data-type=\"link\" data-id=\"https:\/\/erik.thauvin.net\/blog\/posts\/9766\/migrate-gitea-forgejo-from-mariadb-mysql-to-postgresql\">forgejo dump<\/a><\/code> command. However, the import into the PostgreSQL database failed with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ERROR:  stack depth limit exceeded<\/code><\/pre>\n\n\n\n<p>I then tried to import the data and structure into an empty PostgreSQL database with pgloader. Forgejo rejected to start after the the import due to an unexpected database schema.<\/p>\n\n\n\n<p>In the end I started Forgejo with an empty PostgreSQL database and waited until all tables were set up and then stopped the Forgejo process again. To not mess with the production data, I first copied the sqlite file. I then configured pgloader to only import the data. The pgloader config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LOAD DATABASE\nFROM sqlite:\/\/\/root\/forgejo.db\nINTO postgresql:\/\/forgejo:SECRET@127.0.0.1\/forgejo\nWITH data only, reset sequences, prefetch rows = 10000\nSET work_mem TO '16MB', maintenance_work_mem TO '512MB';<\/code><\/pre>\n\n\n\n<p>There were still some problems with columns that did exist in the SQLite database but not in the PostgreSQL database. I simply removed those columns in the copy of the SQLite database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE session DROP COLUMN created_unix;\nALTER TABLE hook_task DROP COLUMN repo_id;<\/code><\/pre>\n\n\n\n<p>Additionally, the <code>keep_activity_private<\/code> column did have some NULL values in the SQLite database. I solved this by first allowing NULL values in the PSQL database, then updating all values, and then again disallowing NULL values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE public.user ALTER COLUMN keep_activity_private DROP NOT NULL;\n# run pgloader now\nUPDATE public.user SET keep_activity_private=TRUE WHERE keep_activity_private IS NULL;\nALTER TABLE public.user ALTER COLUMN keep_activity_private SET NOT NULL;<\/code><\/pre>\n\n\n\n<p>In the end I can definitely recommend the migration. Forgejo is much faster with PSQL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easiest path for migrating Forgejo to PostgreSQL seems to be with the forgejo dump command. However, the import into the PostgreSQL database failed with I then tried to import the data and structure into an empty PostgreSQL database with &hellip; <a href=\"https:\/\/sven-seeberg.de\/wp\/?p=1213\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1213","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1213"}],"version-history":[{"count":4,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":1217,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1213\/revisions\/1217"}],"wp:attachment":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}