Using Supabase for the Postgres Database

Is it possible and has anyone tried using the Postgres SQL database offered by Supabase for the Strapi data source? Any tips on setting this up would be greatly appreciated.

I think it should be possible as supabase provide the DB credentials for direct access but there are often some gotchas in these setups.

Thanks

Dan

I am currently using supabase and have no issues. Just use the connection info under project settings > database > connection info. The install was really straightforward. I am also using supabase for media storage (utilizing the plugin that’s available for that). The README for the supabase upload plugin is not correct. You’ll have to add this section to your config/plugins.js and the associated variables to your .env file.

upload: {
  config: {
    provider: 'strapi-provider-upload-supabase',
    providerOptions: {
      apiUrl: env('SUPABASE_API_URL'),
      apiKey: env('SUPABASE_API_KEY'),
      bucket: env('SUPABASE_BUCKET'),
      directory: env('SUPABASE_DIRECTORY'),
      options: {}
    },
    breakpoints: {
      xlarge: 1920,
      large: 1000,
      medium: 750,
      small: 500,
      xsmall: 64,
    },
  },
},

In addition, I had to add CSP configuration to config\middlewares.js.

{
  name: 'strapi::security',
  config: {
    contentSecurityPolicy: {
      directives: {
        'default-src': ["'self'"],
        'img-src': ["'self'", 'data:', 'blob:', '<YOUR_SUPABASE_DOMAIN>'],
      },
    },
  },
},

See for CSP info:

2 Likes

Thank you, that’s very useful. I was just looking for that confirmation that someone had it working.

Dan

What about using the Supabase user table in stead of the default strapi user table?
That looks a little more complex.

Anyone solved that?

1 Like

I think my approach would be just to use the supabase auth and users for my customer accounts and only use the strapi accounts for internal use in the CMS.

I do hope that this setup will work and not cause conflicts.

I’d love to hear of anyone who has actually gone this way.

Another open question: do the real-time features work if you manage the content with strapi?

Would you be able to detail the layout of your database.js file? Or other plugins / mods needed to make supabase an option for use as a db? :pray:

Were you able to setup Strapi + Supabase Postgres. I am facing an error that Unable to acquire connection from pg module in strapi.

Probably Strapi making so many reqs while migratoin causes this!

From the docs: Database | Strapi Documentation

:warning: Warning
Strapi applications are not meant to be connected to a pre-existing database, not created by a Strapi application, nor connected to a Strapi v3 database. The Strapi team will not support such attempts. Attempting to connect to an unsupported database may, and most likely will, result in lost data.

So im not sure it is supported?

I have created a comprehensive guide on how to deploy strapi DB on supabase. Find it here - https://youtu.be/frjVQ66tQ5Y

There’s an easy way to avoid this by creating a new schema inside of Supabase. In my case simply named ‘strapi’ and you call it out in a environment variable.

then once strapi loads up you run a minor sql script to open up the serverless api and expose the schema.

***STRAPI restricts new users from multiple uploads *** :rage:

after that you should be good to roll as far as development goes.

? file uploads are not restricted

For those who are interested. It is not my recommended approach but I have used supabase both for DB and File Storage.

Here is the example project: GitHub - paulnotfromstrapi/strapi-base

I also used “strapi-provider-upload-supabase”: “^1.0.0” as my file provider.

Here are my config settings.

config/database.js

module.exports = ({ env }) => {
  const client = env("DATABASE_CLIENT");

  const connections = {
    postgres: {
      connection: {
        connectionString: env("DATABASE_URL"),
        ssl: env.bool("DATABASE_SSL", false) && {
          rejectUnauthorized: env.bool(
            "DATABASE_SSL_REJECT_UNAUTHORIZED",
            true
          ),
        },
        schema: env("DATABASE_SCHEMA", "public"),
      },

      pool: {
        min: env.int("DATABASE_POOL_MIN"),
        max: env.int("DATABASE_POOL_MAX"),
      },
    },
  };

  return {
    connection: {
      client,

      ...connections[client],

      acquireConnectionTimeout: env.int("DATABASE_CONNECTION_TIMEOUT", 60000),
    },
  };
};

My supabase provider settings
config/plugins.js

module.exports = ({ env }) => ({
  upload: {
    config: {
      provider: "strapi-provider-upload-supabase",
      providerOptions: {
        apiUrl: env("SUPABASE_API_URL"),
        apiKey: env("SUPABASE_API_KEY"),
        bucket: env("SUPABASE_BUCKET"),
        directory: env("SUPABASE_DIRECTORY"),
        options: {},
      },
      actionOptions: {
        upload: {},
        uploadStream: {},
        delete: {},
      },
    },
  },
});

.env.example

HOST=0.0.0.0
PORT=1337

APP_KEYS=eugbpKpPrDf6pqXMiFk6+w==,TS4NQTj3L6W6FHdm7Bqo1A==,0t/EVFYhX87aEAzNq2x/KA==,jMehGQK4OjiV4i+yKj7jWQ==
API_TOKEN_SALT=ualmALKeYdJlyyVEMcXiQw==
ADMIN_JWT_SECRET=mqYNI4lFWH2BberCaAdoPA==
TRANSFER_TOKEN_SALT=e853xCTd1zTdI26O3NSfVg==

JWT_SECRET=HRsqM7AcFvAa/cctdsqNUQ==

# Supabase Provider
SUPABASE_URL=to_be_modified
SUPABASE_API_URL=to_be_modified
SUPABASE_API_KEY=to_be_modified
SUPABASE_BUCKET=to_be_modified
SUPABASE_DIRECTORY=to_be_modified

# Supabase Database
DATABASE_CLIENT=postgres
DATABASE_URL=to_be_modified
DATABASE_SHEMA=public
DATABASE_POOL_MIN=1
DATABASE_POOL_MAX=100

Although it work fine. I would be curious why folks are using Supabase. If you are just looking for a DB you can checkout something like Neon, a hosted Postgres DB https://neon.tech/.

I may be biased on not using Supabase with Strapi but I know many folks do, here is a video I did showing how to setup supabase and strapi together as a learning exercise.

If you just want to use a hosted DB my recomendation would be to use https://neon.tech/ and then use Cloudinary as image provide.

For anyone interested I have this project where it is deployed to renders free tier ( both Strapi and Astro ) and I use Cloudinary and Neon DB for File storage and Db. Both have very good free tiers.

Project Link

Hope this helps.