Can I have multiple databases?

Hi!

I’m creating a chat system with the example from SocketIO (private messages) to add to my main system (which is created with Strapi).

And I would like to have the messages on a different database but in the same dashboard.

1 Like

Do you mean to display them as Content-types in Strapi’s Admin UI ?

Yep!
I need to make a chat system and I would like to see the messages on the Strapi’s Admin UI on my main system

Strapi allows multiple connections:

module.exports = ({ env }) => ({
  defaultConnection: 'default', // this defines the default connections
  connections: {
    default: { //that's the default connection
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/main.db'), //uses main db
      },
      options: {
        useNullAsDefault: true,
      },
    },
    messages: { //that's the connection for messages db
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/messages.db'), //uses another db
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});

Now in model file config you can define which connection should be used for that model:

{
  "kind": "collectionType",
  "connection": "messages", //use the "default" connection or the "messages" connection.
  "info": {
    "name": "messages",
    "description": "This represents the Messages model"
  },
  "attributes": {
     // ... 
  }
}
5 Likes

And can I create entries with the Admin UI?
Because it shows an error

I can create and list entities with Postman, but I cant use the Admin UI

Currently, no, because of the RBAC feature.

1 Like

I have a few questions…
Is this a frontend problem?
Can I try to make a plugin or something to avoid this error?
Or something that I can do on my second DB?
(I’m not to familiarize with RBAC)

No, unfortunately, it is a backend problem, the RBAC feature expects the strapi_administrator table to be in the same database/connection.

2 Likes

is there any workaround for this ?

I don’t think so
What I did was to make another service with strapi and the main service sends the user data to the other service

FYI we are looking at possibly removing multi-database in v4, will have more information once the RFC is finished.

Not sure to understand, you plan to remove the fact that we can connect more than one database ?

Yes as it’s not natively supported in bookshelf with regards to relations (meaning you can’t have relations that span multiple databases).

Bookshelf doesn’t prefix the database name and if the databases are on two physically different hosts then it’s more or less impossible to join them anyway.

1 Like

@DMehaffy How do you recommend a user connect to 2 databases or to 2 different schemas in the same database (for Postgres)? The use case is that the default schema contains User data and business logic related data and another schema stores analytics based on interactivity/user input. These are separate functional components and made sense to keep them apart for various reasons.

How do we implement such logic on V4?

Feature requested, please vote if you agree - Use multiple database connections | Developer Experience | Strapi

I used to do this in my own application years ago when we supported multiple databases. Short answer is you don’t, long answer is you will have to write your own custom queries using Knex or some other query builder because at this time, from what I’m aware of, we do not intend to implement multiple database support in a single application for the short to medium term future.

If anything we might consider it if we decide to add multi-tenancy. @sunnyson had similar concerns and he might be able to share some of his workarounds as he is doing exactly this right now.

For me this topic is also from interest.
Would be great if @sunnyson could share insights on this topic.

For example one DB containing “massive”-data but is mostly only used to read from with another DB containing strapi + some other “not so massive”-data.

1 Like

In fact, if we have to use strapi as backend for logic, the customer details should be stored in another DB (maybe mongodb) so that logic and customer data are separate

We’re also interested in using multiple databases in the same Strapi application, without needing to write custom queries using Knex.

@DMehaffy, has this that you said back in April '22:

we do not intend to implement multiple database support in a single application for the short to medium term future.

changed by now? Thanks!

The feature request has 32 upvotes now, if you need this feature do upvote!