Asked 1 month ago by StarNavigator142
How can I resolve the 'con.connect is not a function' error when deploying Strapi on Google App Engine?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by StarNavigator142
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm new to both Strapi Cloud and Google Cloud and followed a guide (https://kevinblanco.dev/strapi-cms-on-google-cloud-platform-the-definitive-guide-part-1?source=more_series_bottom_blogs) to deploy my Strapi Cloud app on Google App Engine.
The deployment appears to succeed (see this Terminal output), but when I open the URL, the Strapi admin page does not load (as shown here: Browser error).
The logs show the following error:
BASH2025-02-05 06:24:00 default[xxxxxxxxxxxxxxx] "GET / HTTP/1.1" 503 2025-02-05 06:24:09 default[xxxxxxxxxxxxxxx] [2025-02-05 06:24:09.556] error: con.connect is not a function 2025-02-05 06:24:09 default[xxxxxxxxxxxxxxx] TypeError: con.connect is not a function at Client._connect (/workspace/node_modules/pg/lib/client.js:112:11) at /workspace/node_modules/pg/lib/client.js:166:12 at new Promise (<anonymous>) at Client.connect (/workspace/node_modules/pg/lib/client.js:165:12) at Client_PG._acquireOnlyConnection (/workspace/node_modules/knex/lib/dialects/postgres/index.js:91:23) at Client_PG.acquireRawConnection (/workspace/node_modules/knex/lib/dialects/postgres/index.js:99:17) at create (/workspace/node_modules/knex/lib/client.js:262:39) 2025-02-05 06:24:09 default[xxxxxxxxxxxxxxx] [2025-02-05 06:24:09.557] info: Shutting down Strapi 2025-02-05 06:24:10 default[xxxxxxxxxxxxxxx] "GET /favicon.ico HTTP/1.1" 503 2025-02-05 06:24:16 default[xxxxxxxxxxxxxxx] [2025-02-05 06:24:16.575] error: con.connect is not a function 2025-02-05 06:24:16 default[xxxxxxxxxxxxxxx] TypeError: con.connect is not a function at Client._connect (/workspace/node_modules/pg/lib/client.js:112:11) at /workspace/node_modules/pg/lib/client.js:166:12 at new Promise (<anonymous>) at Client.connect (/workspace/node_modules/pg/lib/client.js:165:12) at Client_PG._acquireOnlyConnection (/workspace/node_modules/knex/lib/dialects/postgres/index.js:91:23) at Client_PG.acquireRawConnection (/workspace/node_modules/knex/lib/dialects/postgres/index.js:99:17) at create (/workspace/node_modules/knex/lib/client.js:262:39) 2025-02-05 06:24:16 default[xxxxxxxxxxxxxxx] [2025-02-05 06:24:16.576] info: Shutting down Strapi
I don't understand what the error means. Below are my configurations:
/backend/app.yaml
YAMLruntime: nodejs20 env: standard instance_class: F2 env_variables: HOST: '0.0.0.0' NODE_ENV: 'production' DATABASE_NAME: 'postgres' DATABASE_USER: 'postgres' DATABASE_PASSWORD: 'xxxxxxxxxxx' INSTANCE_CONNECTION_NAME: 'xxxxxxxxxxx:australia-southeast1:strapi' GCS_BUCKET_NAME: 'xxxxxxxxxxx' GCS_BASE_PATH: 'cms' beta_settings: cloud_sql_instances: 'xxxxxxxxxxx:australia-southeast1:strapi'
/backend/config/env/production/database.js
JAVASCRIPTconst path = require('path'); module.exports = ({ env }) => ({ connections: { client: "postgres", connection: { host: `/cloudsql/${env('INSTANCE_CONNECTION_NAME')}`, database: env('DATABASE_NAME'), user: env('DATABASE_USER'), password: env('DATABASE_PASSWORD'), tmp: path.resolve('/tmp'), }, }, });
/backend/config/database.js
JAVASCRIPTmodule.exports = ({ env }) => { const client = env('DATABASE_CLIENT', 'postgres'); const connections = { postgres: { connection: { client: 'postgres', connection: { host: env('DATABASE_HOST', '127.0.0.1'), port: env.int('DATABASE_PORT', 5432), database: env('DATABASE_NAME', 'postgres'), user: env('DATABASE_USERNAME', 'postgres'), password: env('DATABASE_PASSWORD'), ssl: env.bool('DATABASE_SSL', true), }, pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 350) }, }, }, }; return { connection: { client, ...connections[client], acquireConnectionTimeout: env.int('DATABASE_CONNECTION_TIMEOUT', 600000), }, }; };
/backend/.env
ENVHOST=0.0.0.0 PORT=1337 VITE_PORT=5175 APP_KEYS=xxxxxxxxxxxxxxxxxxxx API_TOKEN_SALT=xxxxxxxxxxxxxxxxxxxx ADMIN_JWT_SECRET=xxxxxxxxxxxxxxxxxxxx TRANSFER_TOKEN_SALT=xxxxxxxxxxxxxxxxxxxx JWT_SECRET=xxxxxxxxxxxxxxxxxxxx DATABASE_URL=postgres://postgres:xxxxxxxxxxxxxxxxxxxx@xx.xxx.xx.xxx:5432/strapi DATABASE_NAME=postgres DATABASE_USER=postgres DATABASE_PASSWORD="xxxxxxxxxxxxxxxxxxxx" DATABASE_HOST=xx.xxx.xx.xxx #public IP address DATABASE_CLIENT=postgres INSTANCE_CONNECTION_NAME=xxxxxxxxxxxxxxxxxxxx:australia-southeast1:strapi GCS_BUCKET_NAME=xxxxxxxxxxxxxxxxxxxx GCS_BASE_PATH=cms CLOUD_SQL_PASSWORD="xxxxxxxxxxxxxxxxxxxx" CLOUD_SQL_CONNECTION_NAME=xxxxxxxxxxxxxxxxxxxx:australia-southeast1:strapi CLOUD_STORAGE_BUCKET_NAME=xxxxxxxxxxxxxxxxxxxx
Expectation:
The browser should display the Strapi admin page.
What I've Tried:
Any guidance on how to fix this error is much appreciated!
Comments 0
•Answers 1
•Views 57
Answer by TychoAI Agent • 1 month ago
Below are several things to check and adjust in your configuration to resolve the error “con.connect is not a function”:
When using Knex (and behind it the pg library), the connection object should only contain connection parameters (like host, port, database, user, password, ssl) without nesting extra properties. For example, instead of nesting a “client” inside the connection object, declare it at the appropriate level.
Incorrect:
JS{ connection: { client: 'postgres', // ← should not be inside "connection" connection: { host: '/cloudsql/INSTANCE_CONNECTION_NAME', // ... }, pool: { … } } }
Correct:
JS{ client: 'postgres', connection: { host: `/cloudsql/${env('INSTANCE_CONNECTION_NAME')}`, database: env('DATABASE_NAME'), user: env('DATABASE_USER'), password: env('DATABASE_PASSWORD'), ssl: { rejectUnauthorized: env.bool('DATABASE_SSL', true) }, }, pool: { min: 2, max: 350 }, acquireConnectionTimeout: env.int('DATABASE_CONNECTION_TIMEOUT', 600000), }
Make sure your production configuration (for example in /backend/config/env/production/database.js
) follows a similar structure.
It looks like you have two database configuration files:
/backend/config/env/production/database.js
/backend/config/database.js
Verify which file Strapi is actually using in production. If both are present, they might conflict. When deploying on App Engine with Cloud SQL, you want to use the configuration that uses a Unix domain socket (e.g. host: /cloudsql/INSTANCE_CONNECTION_NAME
). You may need to remove or adjust one file so that only one, consistent configuration is used.
Check that your environment variable names match your configuration. For example, in your production file you reference:
INSTANCE_CONNECTION_NAME
DATABASE_NAME
DATABASE_USER
DATABASE_PASSWORD
But in your /backend/config/database.js
file you’re referencing DATABASE_USERNAME
for the connection. If your .env file only defines DATABASE_USER
, then DATABASE_USERNAME
might be undefined. This may cause an improperly formed connection config. Update the connection details so that all names match. For instance:
JSconnection: { host: env('DATABASE_HOST', '127.0.0.1'), port: env.int('DATABASE_PORT', 5432), database: env('DATABASE_NAME', 'postgres'), user: env('DATABASE_USER', 'postgres'), // use DATABASE_USER here password: env('DATABASE_PASSWORD'), ssl: env.bool('DATABASE_SSL', true), },
And when using Cloud SQL with App Engine, ensure you override the public IP setting (DATABASE_HOST) with the Unix socket format. One approach is to update your production configuration to ignore DATABASE_HOST
and use the socket path directly.
Before deploying the complete Strapi app, try testing your database connection in a small script with Knex or the pg library. This makes it easier to determine if the issue is with the connection object format or the environment variables. For example:
JSconst { Client } = require('pg'); const client = new Client({ host: `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`, database: process.env.DATABASE_NAME, user: process.env.DATABASE_USER, password: process.env.DATABASE_PASSWORD, ssl: { rejectUnauthorized: process.env.DATABASE_SSL === 'true' }, }); client.connect() .then(() => console.log('Connected successfully')) .catch(err => console.error('Connection error', err)) .finally(() => client.end());
This can help you pinpoint if there’s still an issue with the connection config used by pg.
The error “con.connect is not a function” most likely means that the configuration passed to Knex (and in turn pg) is not structured correctly. By:
• Updating your connection object format,
• Ensuring only one, consistent configuration is used for production, and
• Making sure that your environment variable names are aligned,
you should be able to resolve the issue and have your Strapi admin page show as expected.
Try these steps and let us know if you need further assistance!
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.