You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cloud_sql/postgres/pdo/README.md
+44-16Lines changed: 44 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -2,35 +2,62 @@
2
2
3
3
## Before you begin
4
4
5
-
1. Before you use this code sample, you need to have [Composer](https://getcomposer.org/) installed or downloaded into this folder. Download instructions can be found [here](https://getcomposer.org/download/).
5
+
1. Before you use this code sample, you need to have [Composer](https://getcomposer.org/) installed or downloaded into this folder. Download instructions can be found [here](https://getcomposer.org/download/). Once you've installed composer, use it to install required dependencies by running `composer install`.
6
6
2. Create a PostgreSQL Cloud SQL Instance by following these [instructions](https://cloud.google.com/sql/docs/postgres/create-instance). Note the connection string, database user, and database password that you create.
7
7
3. Create a database for your application by following these [instructions](https://cloud.google.com/sql/docs/postgres/create-manage-databases). Note the database name.
8
8
4. Create a service account with the 'Cloud SQL Client' permissions by following these [instructions](https://cloud.google.com/sql/docs/postgres/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account). Download a JSON key to use to authenticate your connection.
9
-
5. Use the information noted in the previous steps:
9
+
10
+
## Running Locally
11
+
12
+
To run this application locally, download and install the `cloud_sql_proxy` by following the instructions [here](https://cloud.google.com/sql/docs/postgres/sql-proxy#install).
13
+
14
+
To authenticate with Cloud SQL, set the `$GOOGLE_APPLICATION_CREDENTIALS` environment variable:
export DB_HOSTNAME='localhost'# If connecting using TCP instead of Unix Sockets
18
18
```
19
19
20
-
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe.
20
+
To run the Cloud SQL proxy, you need to set the instance connection name. See the instructions [here](https://cloud.google.com/sql/docs/postgres/quickstart-proxy-test#get_the_instance_connection_name) for finding the instance connection name.
21
21
22
-
## Running Locally
22
+
```bash
23
+
export CLOUD_SQL_CONNECTION_NAME='::'
24
+
```
23
25
24
-
To run this application locally, download and install the `cloud_sql_proxy` by following the instructions [here](https://cloud.google.com/sql/docs/postgres/sql-proxy#install).
26
+
Once the proxy is ready, use one of the following commands to start the proxy in the background.
27
+
28
+
You may connect to your instance via either unix sockets or TCP. To connect using a socket, you must provide the `-dir` option when starting the proxy. To connect via TCP, you must provide a port as part of the instance name. Both are demonstrated below.
25
29
26
-
Once the proxy is ready, use the following command to start the proxy in the background:
Note: Make sure to run the command under a user with write access in the `/cloudsql` directory. This proxy will use this folder to create a unix socket the application will use to connect to Cloud SQL.
33
39
40
+
### TCP mode
41
+
42
+
```bash
43
+
$ ./cloud_sql_proxy \
44
+
--instances=$CLOUD_SQL_CONNECTION_NAME=tcp:5432 \
45
+
--credential_file=$GOOGLE_APPLICATION_CREDENTIALS
46
+
```
47
+
48
+
### Set Configuration Values
49
+
50
+
Set the required environment variables for your connection to Cloud SQL. If you are using TCP mode as described above, do not set the `CLOUD_SQL_CONNECTION_NAME` variable.
51
+
52
+
```bash
53
+
export DB_USER='my-db-user'
54
+
export DB_PASS='my-db-pass'
55
+
export DB_NAME='my-db-name'
56
+
export DB_HOSTNAME='localhost'
57
+
```
58
+
59
+
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets safe.
60
+
34
61
Execute the following:
35
62
36
63
```bash
@@ -39,15 +66,16 @@ $ php -S localhost:8080
39
66
40
67
Navigate towards http://localhost:8080 to verify your application is running correctly.
41
68
42
-
## Google App Engine Standard
69
+
## Google App Engine Flex
43
70
44
-
To run on GAE-Standard, create an App Engine project by following the setup for these [instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
71
+
To run on App Engine Flex, create an App Engine project by following the setup for these [instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
45
72
46
73
First, update `app.yaml` with the correct values to pass the environment variables into the runtime.
47
74
75
+
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`.
76
+
48
77
Next, the following command will deploy the application to your Google Cloud project:
0 commit comments