Skip to content

Commit 7c4f468

Browse files
committed
Write azd env
1 parent 2c4762f commit 7c4f468

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.env.azure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Use these values to connect to the Azure database from within the devcontainer
1+
# Set these values to connect to the Azure database
2+
# Use write_azure_env.sh or write_azure_env.ps1 to set these values
23
POSTGRES_DATABASE="db"
34
POSTGRES_HOST="YOUR-SERVER-NAME.postgres.database.azure.com"
45
POSTGRES_SSL="require"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ Follow these steps to deploy a PostgreSQL Flexible Server to Azure with the pgve
100100

101101
This will create a new resource group, and create the PostgreSQL Flexible server inside that group.
102102

103-
1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this command that copies the `azd` environment variables into your local `.env`:
103+
1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this script that copies the necessary `azd` environment variables into your local `.env`:
104104

105105
```shell
106-
azd env get-values > .env
106+
./write_azure_env.sh
107107
```
108108

109109
1. Now you may run the Python scripts in order to interact with the PostgreSQL server.

examples/asyncpg_items.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ async def async_main():
2323
POSTGRES_PASSWORD = os.environ["POSTGRES_PASSWORD"]
2424

2525
DATABASE_URI = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DATABASE}"
26+
# Specify SSL mode if needed
27+
if POSTGRES_SSL := os.environ.get("POSTGRES_SSL"):
28+
DATABASE_URI += f"?sslmode={POSTGRES_SSL}"
2629

2730
conn = await asyncpg.connect(DATABASE_URI)
2831

write_azure_env.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Clear the contents of the .env file
2+
Set-Content -Path .env -Value ""
3+
4+
# Append new values to the .env file
5+
$postgresDatabase = azd env get-value POSTGRES_DATABASE
6+
$postgresHost = azd env get-value POSTGRES_HOST
7+
$postgresSSL = azd env get-value POSTGRES_SSL
8+
$postgresUsername = azd env get-value POSTGRES_USERNAME
9+
10+
Add-Content -Path .env -Value "POSTGRES_DATABASE=$postgresDatabase"
11+
Add-Content -Path .env -Value "POSTGRES_HOST=$postgresHost"
12+
Add-Content -Path .env -Value "POSTGRES_SSL=$postgresSSL"
13+
Add-Content -Path .env -Value "POSTGRES_USERNAME=$postgresUsername"

write_azure_env.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Clear the contents of the .env file
4+
> .env
5+
6+
# Append new values to the .env file
7+
echo "POSTGRES_DATABASE=$(azd env get-value POSTGRES_DATABASE)" >> .env
8+
echo "POSTGRES_HOST=$(azd env get-value POSTGRES_HOST)" >> .env
9+
echo "POSTGRES_SSL=$(azd env get-value POSTGRES_SSL)" >> .env
10+
echo "POSTGRES_USERNAME=$(azd env get-value POSTGRES_USERNAME)" >> .env

0 commit comments

Comments
 (0)