12

I am trying to create an API using Strapi CMS. I have an existing PostgreSQL + postgis database and I would like to connect-use this database in a Strapi project.

Do you know if it is possible to do something like this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Elena Valari
  • 173
  • 2
  • 2
  • 11
  • 1
    I never touched Strapi but If I am not wrong, you can modify the connection string in the Strapi project to match with the existing Postgres DB – Vivek S. Jun 30 '20 at 11:33
  • Thanks for the advice, i just tried to connect strapi with the connection string, there is no errors but strapi cannot "see" the existing tables in the database. Any ideas? Thanks – Elena Valari Jul 01 '20 at 08:16
  • could it be that you're connected to wrong database inside of your postgres instance? – Jim Jones Jul 01 '20 at 09:10
  • Unfortunately no, i have also made some changes to the database.json file in order to define the schema (which is used in postgres db). Here is the database file: { "defaultConnection": "default", "connections": { "default": { "connector": "strapi-hook-bookshelf", "settings": { "client": "postgres", "host": "127.0.0.1", "port": 5432, "database": "Testdb", "schema":"public", "username": "postgres", "password": "$$$$$$" }, "options": {} } } } Any other ideas? Thanks – Elena Valari Jul 02 '20 at 07:40
  • 2
    I've got a similar challenge. This slightly different question seemed to help me https://stackoverflow.com/questions/51171858/strapi-is-not-loading-the-collections-from-existing-mongodb-hosted-on-digital-oc – Mark Collins Jul 03 '20 at 10:41
  • **You can't**, as far as I'm aware. If you are looking for a **schema-agnostic headless CMS** try [Directus](https://directus.io/) Also, haven't tried it yet, but should take a look at this **PostgreSQL based firebase alternative**, [Supabase](https://supabase.io/) – Otoniel Guajardo Nov 02 '20 at 14:16
  • You can't - check this statement on this Strapi page 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. https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure – Manuela Jan 12 '23 at 11:32

4 Answers4

8

I was looking for an answer for this also, but it seems the only answer is no, because Strapi is not schema-agnostic: you must adopt their database structure to use Strapi.

Now if I could only figure out a way to ask Stack Overflow for a schema-agnostic headless CMS ... without getting it flagged as an invalid/subjective question ...

machineghost
  • 33,529
  • 30
  • 159
  • 234
5

You can do it while you create a strapi app just choose custom and setup the database there. and to import existing tables from that db just create a mock table using strapi admin panel and in /api folder in your project root folder and copy paste the table data you just created in api folder and chage the name of that folder to the table name and do same for the files in side that folder and,

in the api>(your table folder)>config>routes.json file change the name of table inside I will add a link to help you better understand.

for setup in strapi with existing database> configure-strapi-postgresql

I couldn't find the tut for setting up exist but this is for mysql setps are smilers.

strapi with sql

ovyas24
  • 63
  • 1
  • 6
  • please be aware of this warning on the strapi page: 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. https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure – Manuela Jan 12 '23 at 11:33
3

The best way is to create a new database with the same structure but created with strapi and after migrate the data.

0

You are creating an API using Strapi CMS. There is no reason why your database should be Strapi database.

Just build your API with Strapi.

Or you can use this tutorial to integrate Strapi into your existing database as @Augusto Rodriguez said.

Hrvoje
  • 13,566
  • 7
  • 90
  • 104