Migrate current Database

System Information
  • Strapi Version: “4.15.4”
  • Operating System: “Mac OS 12.7.1 (Monterey)”
  • Database: “sqlite”
  • Node Version: “v18.16.1”
  • NPM Version: “9.5.1”
  • Yarn Version:

Hello friends, I am new to strapi, my question is the following, can I migrate my current sqlite database to postgresql, I already have information in it

Yes, you can accomplish this by using our export / import feature.

In your local setup while running SQLite use the following command to make a backup of your data.

Here I have a project with simple post collection type.

Currently my project is using SQLite.

I am going to use the following command to export my data.

   yarn strapi export --no-encrypt -f backup

Now update your ./config/database.js file to point to a postgresql instance and update accordingly.

Install pg vith yarn add pg to add postgres support. pg - npm

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST', '127.0.0.1'),
      port: env.int('DATABASE_PORT', 5432),
      database: env('DATABASE_NAME', 'strapi'),
      user: env('DATABASE_USERNAME', 'strapi'),
      password: env('DATABASE_PASSWORD', 'strapi'),
      schema: env('DATABASE_SCHEMA', 'public'), // Not required
      ssl: env.bool('DATABASE_SSL', false) && {
        rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
      },
    },
    debug: false,
  },
});

Once you make these changes, run the following command.

yarn build && yarn develop

Because you are now pointing to a fresh postgres db you will need to create a new admin user.

Once logged in, you will see that you don’t have any data.

Now let’s restore our data with the import command from our recently created backup file.

You can run the following command.

   yarn strapi import -f backup.tar.gz

After restarting your Strapi App, you should now be able to see your data.

Now we can double check out postgres db to confirm the migration.

You can now see that I am now using my postgres db with my previous data.

Hope this helps.

2 Likes

Thank you very much

I would add that you should be aware that lifecycle functions will run during import.