connect strapi to postgresql on railway.app

28 Views Asked by At

would like to know, how I can connect my strapi project (that I have on my github and have deployed it on railway.app) with a PostgreSQL database that I also created on railway.app.

So far I have tried to use the variables on my strapi project (on railway.app):

ADMIN_JWT_SECRET *******


API_TOKEN_SALT *******


APP_KEYS *******


HOST *******


JWT_SECRET *******


PORT *******


TRANSFER_TOKEN_SALT *******


strapi variables on railway.app

and also my postgreSQL db on railway.app with the following variables:

DATABASE_PRIVATE_URL *******


DATABASE_URL *******


PGDATA *******


PGDATABASE *******


PGHOST *******


PGPASSWORD *******


PGPORT *******


PGPRIVATEHOST *******


PGUSER *******


POSTGRES_DB *******


POSTGRES_PASSWORD *******


POSTGRES_USER *******


SSL_CERT_DAYS *******


Postgresql db on railway.app with variables

I also have a local strapi project with the following database:

config/database.js

const path = require('path');

module.exports = ({ env }) => {
  const client = env('DATABASE_CLIENT', 'sqlite');

  const connections = {
    postgres: {
      connection: {
        connectionString: env('DATABASE_URL'),
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 5432),
        database: env('DATABASE_NAME', 'strapi'),
        user: env('DATABASE_USERNAME', 'strapi'),
        password: env('DATABASE_PASSWORD', 'strapi'),
        ssl: env.bool('DATABASE_SSL', false) && {
          key: env('DATABASE_SSL_KEY', undefined),
          cert: env('DATABASE_SSL_CERT', undefined),
          ca: env('DATABASE_SSL_CA', undefined),
          capath: env('DATABASE_SSL_CAPATH', undefined),
          cipher: env('DATABASE_SSL_CIPHER', undefined),
          rejectUnauthorized: env.bool(
            'DATABASE_SSL_REJECT_UNAUTHORIZED',
            true
          ),
        },
        schema: env('DATABASE_SCHEMA', 'public'),
      },
      pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
    },
  };

  return {
    connection: {
      client,
      ...connections[client],
      acquireConnectionTimeout: env.int('DATABASE_CONNECTION_TIMEOUT', 60000),
    },
  };
};

and the .env file:

HOST=0.0.0.0
PORT=1337
APP_KEYS="toBeModified1,toBeModified2"
API_TOKEN_SALT=tobemodified
ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
JWT_SECRET=tobemodified

HOST=0.0.0.0
PORT=1337
APP_KEYS=tobemodified
API_TOKEN_SALT=tobemodified
ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
# Database
DATABASE_URL=tobemodified
DATABASE_CLIENT=tobemodified
DATABASE_HOST=tobemodified
DATABASE_PORT=tobemodified
DATABASE_NAME=tobemodified
DATABASE_USERNAME=tobemodified
DATABASE_PASSWORD=tobemodified
DATABASE_SSL=false
JWT_SECRET=tobemodified

What I would like to achieve:

  1. Access my PostgreSQL db from my local strapi project
  2. Access my PostgreSQL db from the deployed strapi project (both are on Railway.app)

please help

1

There are 1 best solutions below

0
zuchka On

Railway Community Engineer here. Have you tried using the Strapi template? I would try that, or at least deploy it and copy their pattern. But that template is everything you want:

https://railway.app/template/strapi

After you've deployed the template, follow these steps to "eject" from the new template deploy process. This will fork the repo to your GH account.

https://railway.app/changelog/2024-03-22-eject-services-templates#contextual-help-requests

after that, any changes you merge into your forked repo will push to Railway automatically.

As far as working on your project locally, you can use the Railway CLI to connect to your DB on Railway and develop from there:

https://docs.railway.app/guides/cli

If you need any more help, I'd make a post in Help Station