How to deploy Strapi with Postgresql on Google App engine

Rahul Bharati
3 min readMar 1, 2022

In this post, we’ll cover how to deploy Strapi + PostgreSQL App on Google App Engine or GAE. Strapi is one of the amazing headless CMS out there and many developers had made the switch from MySQL to Postgresql.

A quick intro to Strapi

Strapi is an open-source headless CMS used to build API and reduce overhead while developing the backend. It is highly customizable and used widely. Strapi is self-hosted and can be used with most of the Relational Database systems allowing users to be flexible with their needs.

A quick intro to App

Google App Engine (often referred to as GAE or simply App Engine) is a cloud computing platform for developing and hosting web applications in Google-managed data centers. It provides an infrastructure for building scalable applications and is run in a sandbox environment having access to other GCP products.

Without any further ado, let's dive into how to set up and deploy Strapi + Postgresql App to App Engine.

Create a new Strapi project

To create a new Strapi project run npx create-strapi-app mp-app --quickstartif using npm or yarn create strapi-app my-app --quickstartif using yarn. Once done quit the running app and make the initial commit.

Install Google Cloud CLI

If you have not installed google cloud CLI then it is the right time to install it now. Install Google cloud client from https://cloud.google.com/sdk/docs/install

Once installed initialize google cloud CLI by following this guide https://cloud.google.com/sdk/docs/initializing

Create App Engine on Google Cloud

Create an App Engine project at https://console.cloud.google.com/appengine/
Select your desired region. Once done then move to the next step.

Create Postgresql Instance

Create a Postgres instance at https://console.cloud.google.com/sql/instances provide required details and take note of the password we’ll use this later. Once the instance is created, create the database for your Strapi App that you’ll use.

Config setup for App Engine

  1. Create app.yaml in your Strapi root project and add the following content.

For Standard environment

runtime: nodejs16instance_class: F2env_variables:
NODE_ENV: 'production'
beta_settings:
cloud_sql_instances: '<instance_identifier>'
service_account: '<service-account-id>'

For Flex Environment

runtime: nodejsenv: flexenv_variables:
NODE_ENV: 'production'
beta_settings:
cloud_sql_instances: '<instance_identifier>'
service_account: '<service-account-id>'

Here <instance_identifier> is your Postgres instance-identifier which will look like <project-id:region:instance_name> and can be found on your SQL dashboard. And <service-account-id> should be the service account that has access to both Postgresql instance and App Engine instance. For more settings visit https://cloud.google.com/appengine/docs/standard/nodejs/config/appref

2. Update .env file

Update your .env file with following key-value pairs

HOST=0.0.0.0
DATABASE_NAME=[database_name]
DATABASE_USER=postgres
DATABASE_PASSWORD=[password you generated]
INSTANCE_CONNECTION_NAME=<instance_identifier>

3. Create .gcloudignore file

Create .gcloudignore file and add following contents

.gcloudignore
.git
.gitignore
node_modules/
#!include:.gitignore
!.env
yarn.lock
package-json.lock

4. Create .gitkeep

Create .gitkeep file inside database/migrations folder this will allow empty migration folder to be stored in GitHub and deployed to App Engine since App Engine doesn’t have make directory access.

Now let’s set up Postgresql with Strapi

  1. Install pg package using npm install pg or yarn add pg
  2. Update Strapi database config to use Postgresql. Edit file config/env/production/database.js and update with following content
module.exports = ({ env }) => ({
connection: {
client: "postgres",
connection: {
host: `/cloudsql/${env("INSTANCE_CONNECTION_NAME")}`,
database: env("DATABASE_NAME"),
user: env("DATABASE_USER"),
password: env("DATABASE_PASSWORD"),
},
},
});

3. Update package.json file to auto-build Strapi app when deployed. Update following entries in script section

"scripts": {
"develop": "strapi develop",
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi",
"gcp-build": "strapi build"
},

Let’s deploy

Now it’s time to deploy our app to Google App Engine. To deploy run the following command in your terminal.

gcloud app deploy app.yaml --project [project-id]

Once deployment is successful you can visit your site at https://<app-engine-url>/admin/

--

--

I’m a full-stack developer (MERN). I also like to read books, play guitar, and sketch.