2
  1. Strapi works locally if im running sqlite in the database.js but not if I'm running postgres / I've found online that I can specify that npm run develop uses sqlite, and production should use postgres.

For REF - I found this answer here: https://github.com/strapi/strapi/discussions/6832

Can anyone show me how to set this up as I am really finding it hard to read the docus for this issue.

Currently in the file structure:

config/database.js

I have these two set ups (for local and heroku) - I comment out the postgres set up for heroku to work locally

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/data.db'),
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});


module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: env('DATABASE_HOST', '127.0.0.1'),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME', 'strapi'),
        username: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        ssl: false,
      },
    },
  },
});

  1. When I pushed my strapi project to Heroku the correct structure is there i.e. names of articles (SHOWN IN THE BELOW IMAGE) - but none of the content is there: i.e. posts, images etc

enter image description here

What I imagine is happening is because the local strapi has been built using sqlite and heroku required me to use postgres as a database, the databases are not the same so the data is not being read correctly?

(I could be wrong about that....)

In this case: how do you move your local dev (recommended quick start set up) to your production site?

  • Do you npm run build? and then host strapi on your normal front end site?

  • is there a way to move the data from one database to another?

Sorry for asking many small questions here - I might have missed a concept which ties it all together.

Thanks in advance for any help, Wally

Wally
  • 705
  • 1
  • 9
  • 24

3 Answers3

5

I found a very useful Youtube video that actually explains this process for step 1)

How to run a different database depending if it is being used by dev or production.

Short answer is in the database.js file you write an if statement to see if your using dev or production:

Once I have implemented this I will write a full answer :)

VIDEO LINK: https://www.youtube.com/watch?v=xNE0TrI5OKk


PART 2: Can you migrate the work you have done in strapi to a production database, for example Heroku....?

Simple answer is sadly (and hella frustrating) - NO!

This is from Strapis site:

Does Strapi handle deploying or migrating of content? Strapi does not currently provide any tools for migrating or deploying your data changes between different environments (ie. from development to production). With the exception being the Content-Manager settings, to read more about this option please see the following CLI documentation.

FOUND HERE: https://strapi.io/documentation/v3.x/getting-started/troubleshooting.html#frequently-asked-questions


I've also been speaking with a really helpful rep on the Strapi Slack page to figure out WTF I've been doing wrong (turns out allot.....).

Anyway:

  • Create your content types in your local strapi
  • Push them to your online strapi (Heroku)
  • Add all content online NOT locally.....

SMALL RAY OF HOPE?

I have not done this yet but apparently you can manually transfer the data and convert it from sqlite to postgres by using:

You will have to do a data dump using some local DB client (DBeaver supports SQLite) then import that data onto the PG server (you can also use DBeaver there) Strapi doesn't have any tooling or suggestions for content migration between environments currently.

Well I hope this helps anyone else who comes across this issue like me....

Wally :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wally
  • 705
  • 1
  • 9
  • 24
1

You can point both Strapis (dev and production) to use the same database, in your case Heroku's Postgres. If the both uses the same database, then modifications made to datastructure and content in dev-strapi are available also in production. Datastructure (for ex. new content type) won't be available until you push a new version to Heroku, but the new content using the old datastructures will be available in production-strapi also (both strapis use the same database -> same data).

EDIT: following lines are not valid anymore, the newest version of strapi uses little bit different approach for configuring database connection (see video link from another answer)

You can define database connections in dev-strapi:
\config\env\development\database.json
\config\env\production\database.json

EDIT ENDS

In Heroku's Postgres I believe you have made env-variables for database connection. You can make same variables for dev-version by creating .env-file (you'll find all info neede from Heroku's variables):

DATABASE_HOST=xxx-yyy-zzz.eu-west-1.compute.amazonaws.com
DATABASE_PORT=XXYY
DATABASE_NAME=xxxyyyxxx
DATABASE_USERNAME=xxxxxxx
DATABASE_PASSWORD=123123123xyzxyz
grohjy
  • 2,059
  • 1
  • 18
  • 19
  • Hi @grohjy - thank you for your input - I was going to say that the above seems to be from a slightly older version of Strapi, but thank you so much for your input! In your example you mention that if you start off using strapi in postslq and in heroku there are no conflicts with the data base - but do you have any ideas about if you started in sqlite and now have to use postslq for heroku? thanks in advance, Wally – Wally Aug 10 '20 at 13:51
  • I believe you can point your dev-strapi to use the production-strapi’s database anytime. I haven’t tested will the datastructures, if they are modified in dev-strapi (using sqllite), update in production database, when redeployd. I believe they will and if that is the case there is no need to use the same database. – grohjy Aug 10 '20 at 14:13
  • Do you think that would work when the dev is in sqlite and the production is in postsql? (I don't know myself, so am just asking if that even matters that they are different DB's) -- Also would you have an example where I would do that from? – Wally Aug 10 '20 at 14:35
  • There should be no issue to change the database or use different db for dev-strapi than production-strapi. Only data (=content) migration is not supported ie. If you create content in dev-strapi there is no automatic way to move that content to production version. Datastructures should update automatically, when you build and deploy a new version for production. Read more: https://github.com/strapi/strapi/issues/205 – grohjy Aug 10 '20 at 18:04
1

sounds like you are actually wanting to migrate your data between environments... this is not currently supported by strapi. you can try finding or writing your own db seed file to move the data between environments.

dont use the same db from different environments.

this was already answered correctly, not sure why no correct answer has been selected.

Muhammad Ahmod
  • 649
  • 4
  • 15
  • thanks for the input - no answer as stack overflow has a time limit on selecting your own researched answer and I was busy with other projects – Wally Aug 22 '20 at 08:45