Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cloud Run API to Cloud SQL (Postgres) - Unable to connect.

I must be doing something wrong and I appreciate any help I can get with this.

This is what is happening:

I created a simple hello world cloud run api, and that worked just fine.

I then tried to connect to the postgres cloud sql database I created and wanted to return a SELECT NOW(). 

 

This is what code assist says about the error:

server.ts code in the immersive is configured to use the corresponding socketPath: '/cloudsql/test:us-west1:test-db', the ECONNREFUSED 127.0.0.1:5432 error you are still getting strongly points back to the Unix domain socket file not being ready or accessible when your application attempts to connect, leading to the pg library's fallback TCP connection.

"certificate had CN "", expected "

 

// const dbConfig = {
//   user: 'username',
//   socketPath: '/cloudsql/test:us-west1:test-db', //  Name
//   database: 'dbname',
//   password: '',
// };
 
I have been trying to use code assist, gemini, and we tried so many different solutions we ended up going in circles with no success.
 
Does socketPath work? Is there another alterative to connect to this database? I have been trying for 2 days to establish a connection and for some reason I am failing.
 
I made sure there was no environment variables, and also made sure the database is in the sql connection.
 
I am completely stumped.
 
 
 
 
 
 

 

Solved Solved
0 1 98
1 ACCEPTED SOLUTION

I was able to figure it out. There was an additional error that provided some indication of the issue.

"certificate had CN "", expected "

 

1) I changed my connection settings:

  user: 'postgres', // Replace with your PostgreSQL database user
  host: '/cloudsql/test:us-west1:test-postgre-dev', // <-- REPLACE with your actual Instance Connection Name
  database: 'postgres', // Replace with your PostgreSQL database name
  password: 'yourpass', // <-- REPLACE with your actual database password
 

2) I had to recreated my database and choose:

Google managed internal certificate authority
An internal per-instance certificate authority is the trust anchor for the instance.

This is located in the server certificate authority mode. By default when you create the database it is marked as Google managed CAS certificate authority. And with the most recent instructions it will fail.

I hope this helps anyone that may encounter this issue.

Manage server certificate authority mode

Google managed internal certificate authority
An internal per-instance certificate authority is the trust anchor for the instance.
Google managed CAS certificate authority
A root certificate authority and subordinate certificate authorities stored in Certificate Authority Service (CAS) are the trust anchors for all instances in a region.
Customer managed CAS certificate authority
Certificate authorities that you create and manage in the Certificate Authority Service (CAS) are the trust anchors for the instance. This option is available only through gcloud CLI and API.

 

View solution in original post

1 REPLY 1

I was able to figure it out. There was an additional error that provided some indication of the issue.

"certificate had CN "", expected "

 

1) I changed my connection settings:

  user: 'postgres', // Replace with your PostgreSQL database user
  host: '/cloudsql/test:us-west1:test-postgre-dev', // <-- REPLACE with your actual Instance Connection Name
  database: 'postgres', // Replace with your PostgreSQL database name
  password: 'yourpass', // <-- REPLACE with your actual database password
 

2) I had to recreated my database and choose:

Google managed internal certificate authority
An internal per-instance certificate authority is the trust anchor for the instance.

This is located in the server certificate authority mode. By default when you create the database it is marked as Google managed CAS certificate authority. And with the most recent instructions it will fail.

I hope this helps anyone that may encounter this issue.

Manage server certificate authority mode

Google managed internal certificate authority
An internal per-instance certificate authority is the trust anchor for the instance.
Google managed CAS certificate authority
A root certificate authority and subordinate certificate authorities stored in Certificate Authority Service (CAS) are the trust anchors for all instances in a region.
Customer managed CAS certificate authority
Certificate authorities that you create and manage in the Certificate Authority Service (CAS) are the trust anchors for the instance. This option is available only through gcloud CLI and API.