Migrate nextcloud database

This post describes how to migrate the nextcloud database from one database type to another. In this particular case I describe the migration from MySQL / MariaDB to Postgresql.

Beside the database itself the client software and the PHP bindings are required. You can install them with the following command

apt install postgresql postgresql-contrib php7.3-pgsql

As the next step, a user and a database for the nextcloud instance is required.

sudo su - postgres
psql 
CREATE USER nextcloud WITH PASSWORD 'your-password';
\q

createdb --owner nextcloud --encoding UTF-8 nextcloud

Now the migration itself can be started. Since the migration will take while depnding on the size of your Nextcloud instance. Start the migration in a within a screen or tmux environment. The password of the postgres user is entered on the command line. Make sure to clear the history before exiting the screen environment. Otherwise the password will be stored in the history file – e.g. .bash_history.

screen
sudo -u www-data php7.3 /var/www/html/nextcloud/occ db:convert-type --port 5432 --all-apps --password "<your-password>" pgsql nextcloud localhost nextcloud
history -c
exit

The migration also automatically updates the config file. So nextcloud is running on Postgresql right after the migration has finished. You may need to restart the webserver.

systemctl restart apache2