File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -203,6 +203,81 @@ def handle(event, context):
203203 }
204204```
205205
206+ ### Example with Postgresql:
207+
208+ stack.yml
209+
210+ ``` yaml
211+ version : 1.0
212+ provider :
213+ name : openfaas
214+ gateway : http://127.0.0.1:8080
215+ functions :
216+ pgfn :
217+ lang : python3-http-debian
218+ handler : ./pgfn
219+ image : pgfn:latest
220+ build_options :
221+ - libpq
222+ ` ` `
223+
224+ Alternatively you can specify ` ADDITIONAL_PACKAGE` in the `build_args` section for the function.
225+
226+ ` ` ` yaml
227+ build_args:
228+ ADDITIONAL_PACKAGE: "libpq-dev gcc python3-dev"
229+ ` ` `
230+
231+ requirements.txt
232+
233+ ```
234+ psycopg2==2.9.3
235+ ```
236+
237+ Create a database and table:
238+
239+ ```sql
240+ CREATE DATABASE main;
241+
242+ \c main;
243+
244+ CREATE TABLE users (
245+ name TEXT,
246+ );
247+
248+ -- Insert the original Postgresql author's name into the test table:
249+
250+ INSERT INTO users (name) VALUES ('Michael Stonebraker');
251+ ```
252+
253+ handler.py:
254+
255+ ``` python
256+ import psycopg2
257+
258+ def handle (event , context ):
259+
260+ try :
261+ conn = psycopg2.connect(" dbname='main' user='postgres' port=5432 host='192.168.1.35' password='passwd'" )
262+ except Exception as e:
263+ print (" DB error {} " .format(e))
264+ return {
265+ " statusCode" : 500 ,
266+ " body" : e
267+ }
268+
269+ cur = conn.cursor()
270+ cur.execute(""" SELECT * from users;""" )
271+ rows = cur.fetchall()
272+
273+ return {
274+ " statusCode" : 200 ,
275+ " body" : rows
276+ }
277+ ```
278+
279+ Always read the secret from an OpenFaaS secret at ` /var/openfaas/secrets/secret-name ` . The use of environment variables is an anti-pattern and will be visible via the OpenFaaS API.
280+
206281# Using the python3-flask template
207282
208283Create a new function
You can’t perform that action at this time.
0 commit comments