- Before you use this code sample, you need to have
Composer installed or downloaded into this folder.
Download instructions can be found here.
Once you've installed composer, use it to install required dependencies by
running
composer install. - Create a PostgreSQL Cloud SQL Instance by following these instructions. Note the connection string, database user, and database password that you create.
- Create a database for your application by following these instructions. Note the database name.
- Create a service account with the 'Cloud SQL Client' permissions by following these instructions. Download a JSON key to use to authenticate your connection.
To run this application locally, download and install the cloud_sql_proxy by
following the instructions
here.
Instructions are provided below for using the proxy with a TCP connection or a Unix domain socket. On Linux or macOS, you can use either option, but the Windows proxy requires a TCP connection.
NOTE: this option is currently only supported on Linux and macOS. Windows users should use the TCP option.
To use a Unix socket, you'll need to create a directory and give access to the user running the proxy:
sudo mkdir /path/to/the/new/directory
sudo chown -R $USER /path/to/the/new/directoryYou'll also need to initialize an environment variable pointing to the directory you just created:
export DB_SOCKET_DIR=/path/to/the/new/directoryUes these terminal commands to initialize other environment variables as well:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
export DB_USER='<DB_USER_NAME>'
export DB_PASS='<DB_PASSWORD>'
export DB_NAME='<DB_NAME>'Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Secret Manager to help keep secrets safe.
Then use the following command to launch the proxy in the background:
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$CLOUD_SQL_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &To run the sample locally with a TCP conneciton, set environment variables and launch the proxy as shown below.
Use these terminal commands to initalie environment variables:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
export DB_HOST='127.0.0.1'
export DB_USER='<DB_USER_NAME>'
export DB_PASS='<DB_PASSWORD>'
export DB_NAME='<DB_NAME>'Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Secret Manager to help keep secrets safe.
Then use the following command to launch the proxy in the background:
./cloud_sql_proxy -instances=$CLOUD_SQL_CONNECTION_NAME=tcp:5432 -credential_file=$GOOGLE_APPLICAITON_CREDENTIALS &Use these PowerShell commands to initialize environment variables:
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
$env:DB_HOST="127.0.0.1"
$env:DB_USER="<DB_USER_NAME>"
$env:DB_PASS="<DB_PASSWORD>"
$env:DB_NAME="<DB_NAME>Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Secret Manager to help keep secrets safe.
Then use the following command to launch the proxy in a seperate PowerShell session:
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<project-id>:<region>:<instance-name>=tcp:5432 -credential_file=<CREDENTIALS_JSON_FILE>"Execute the following to start the application server:
$ php -S localhost:8080Navigate towards http://localhost:8080 to verify your application is running correctly.
Note: App Engine Standard does not support TCP connections to Cloud SQL instances, only Unix socket connections.
To run on App Engine Standard, create an App Engine project by following the setup for these instructions.
First, update app.standard.yaml with the correct values to pass the
environment variables into the runtime.
Next, the following command will deploy the application to your Google Cloud project:
$ gcloud app deploy app.standard.yamlTo run on App Engine Flex, create an App Engine project by following the setup for these instructions.
First, update app.flex.yaml with the correct values to pass the environment
variables into the runtime.
To use a TCP connection instead of a Unix socket to connect your sample to your
Cloud SQL instance on App Engine, make sure to uncomment the DB_HOST
field under env_variables. Also make sure to remove the uncommented
beta_settings and cloud_sql_instances fields and replace them with the
commented beta_settings and cloud_sql_instances fields.
Then, make sure that the service account
service-{PROJECT_NUMBER}>@gae-api-prod.google.com.iam.gserviceaccount.com has
the IAM role Cloud SQL Client.
Next, the following command will deploy the application to your Google Cloud project:
$ gcloud beta app deploy app.flex.yaml