SlideShare a Scribd company logo
PostgreSQL and
Python
henning.jacobs@zalando.de
Well known...
import psycopg2
conn = psycopg2.connect('dbname=mydb user=myusr')
cur = conn.cursor()
cur.execute('SELECT * FROM mytable')
row = cur.fetchone()
PostgreSQL speaks Python!
CREATE FUNCTION greet(name TEXT) RETURNS TEXT AS
$$
import random
attr = random.choice([
'great', 'fabulous', 'bright', 'brave'])
return 'Hello {} {}!'.format(attr, name)
$$
LANGUAGE plpythonu;
SELECT greet('Python fan');
PostgreSQL speaks Python
● PL/Python
– “just” another procedural language
● SQL script that contains a string of Python
● Allows using any Python module
Real World Examples...
mydb=# SELECT * FROM get_server_connection_stats() LIMIT 5;
server_addr | server_port | client_addr | client_port
---------------+-------------+--------------+-------------
10.160.26.128 | 5435 | 10.111.74.16 | 40524
10.160.26.128 | 5435 | 10.160.26.1 | 49681
10.160.26.128 | 5435 | 10.160.26.1 | 15456
10.160.26.128 | 5435 | 10.160.26.2 | 64136
10.160.26.128 | 5435 | 10.160.26.1 | 1938
(5 rows)
Expose Stats via SQL with Python
CREATE FUNCTION get_server_connection_stats()
RETURNS SETOF server_connection AS
$$
# …
try:
with open('/proc/net/tcp') as fp:
lines = fp.read().splitlines()
except:
plpy.error('Unable to read /proc/net/tcp')
# …
return result
$$
LANGUAGE plpythonu;
Custom JSON Type Validation
CREATE TRIGGER validation AFTER INSERT OR UPDATE
ON config_value FOR EACH …
CREATE FUNCTION validate(value JSON, type_id INT)
RETURNS VOID AS
$$
import json
# … complex type checking here
# … using all of Python's expressiveness :-)
raise ValueError('{} not valid'.format(value))
$$
LANGUAGE plpythonu;
tech.zalando.com

More Related Content

PDF
Python postgre sql a wonderful wedding
Stéphane Wirtel
 
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
PDF
Programming with Python and PostgreSQL
Peter Eisentraut
 
PDF
はじめてのGroovy
Tsuyoshi Yamamoto
 
PDF
Elm: give it a try
Eugene Zharkov
 
PDF
Cycle.js: Functional and Reactive
Eugene Zharkov
 
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
PPTX
ES6 in Real Life
Domenic Denicola
 
Python postgre sql a wonderful wedding
Stéphane Wirtel
 
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
Programming with Python and PostgreSQL
Peter Eisentraut
 
はじめてのGroovy
Tsuyoshi Yamamoto
 
Elm: give it a try
Eugene Zharkov
 
Cycle.js: Functional and Reactive
Eugene Zharkov
 
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
ES6 in Real Life
Domenic Denicola
 

What's hot (18)

PPTX
2017 02-07 - elastic & spark. building a search geo locator
Alberto Paro
 
PDF
Pdxpugday2010 pg90
Selena Deckelmann
 
PDF
多治見IT勉強会 Groovy Grails
Tsuyoshi Yamamoto
 
PDF
Mozilla とブラウザゲーム
Noritada Shimizu
 
PDF
Cooking pies with Celery
Aleksandr Mokrov
 
PPTX
ElasticSearch 5.x - New Tricks - 2017-02-08 - Elasticsearch Meetup
Alberto Paro
 
PDF
Javascript ES6 generators
RameshNair6
 
PPTX
2015 555 kharchenko_ppt
Maxym Kharchenko
 
PDF
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Víctor Bolinches
 
PPTX
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QAFest
 
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
PDF
Jggug 2010 330 Grails 1.3 観察
Tsuyoshi Yamamoto
 
PDF
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Tsuyoshi Yamamoto
 
PDF
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
PPTX
Commit2015 kharchenko - python generators - ext
Maxym Kharchenko
 
PDF
The Browser Environment - A Systems Programmer's Perspective
Eleanor McHugh
 
PDF
PostgreSQL and PL/Java
Peter Eisentraut
 
PDF
6. Generics. Collections. Streams
DEVTYPE
 
2017 02-07 - elastic & spark. building a search geo locator
Alberto Paro
 
Pdxpugday2010 pg90
Selena Deckelmann
 
多治見IT勉強会 Groovy Grails
Tsuyoshi Yamamoto
 
Mozilla とブラウザゲーム
Noritada Shimizu
 
Cooking pies with Celery
Aleksandr Mokrov
 
ElasticSearch 5.x - New Tricks - 2017-02-08 - Elasticsearch Meetup
Alberto Paro
 
Javascript ES6 generators
RameshNair6
 
2015 555 kharchenko_ppt
Maxym Kharchenko
 
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Víctor Bolinches
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QAFest
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Jggug 2010 330 Grails 1.3 観察
Tsuyoshi Yamamoto
 
Groovy ネタ NGK 忘年会2009 ライトニングトーク
Tsuyoshi Yamamoto
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
MongoDB
 
Commit2015 kharchenko - python generators - ext
Maxym Kharchenko
 
The Browser Environment - A Systems Programmer's Perspective
Eleanor McHugh
 
PostgreSQL and PL/Java
Peter Eisentraut
 
6. Generics. Collections. Streams
DEVTYPE
 
Ad

Viewers also liked (18)

PDF
PostgreSQL - C言語によるユーザ定義関数の作り方
Satoshi Nagayasu
 
PDF
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
ODP
Rethink db with Python
Prabhu Raghav
 
PPTX
Succumbing to the Python in Financial Markets
dcerezo
 
PDF
MySQL User Conference 2009: Python and MySQL
Ted Leung
 
PDF
Scaling mysql with python (and Docker).
Roberto Polli
 
PPTX
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
PDF
Python for Derivative Analytics
Alicia G
 
PDF
Python Utilities for Managing MySQL Databases
Mats Kindahl
 
PDF
PostgreSQLとPythonとSQL
Satoshi Yamada
 
PPTX
Python for Big Data Analytics
Edureka!
 
PPTX
Python for Big Data Analytics
Edureka!
 
PDF
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
PPTX
Python PPT
Edureka!
 
PDF
Orchestrating Docker containers at scale
Maciej Lasyk
 
ODP
Python Presentation
Narendra Sisodiya
 
PPT
Introduction to Python
Nowell Strite
 
PostgreSQL - C言語によるユーザ定義関数の作り方
Satoshi Nagayasu
 
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
Rethink db with Python
Prabhu Raghav
 
Succumbing to the Python in Financial Markets
dcerezo
 
MySQL User Conference 2009: Python and MySQL
Ted Leung
 
Scaling mysql with python (and Docker).
Roberto Polli
 
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
Python for Derivative Analytics
Alicia G
 
Python Utilities for Managing MySQL Databases
Mats Kindahl
 
PostgreSQLとPythonとSQL
Satoshi Yamada
 
Python for Big Data Analytics
Edureka!
 
Python for Big Data Analytics
Edureka!
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
Python PPT
Edureka!
 
Orchestrating Docker containers at scale
Maciej Lasyk
 
Python Presentation
Narendra Sisodiya
 
Introduction to Python
Nowell Strite
 
Ad

Similar to "PostgreSQL and Python" Lightning Talk @EuroPython2014 (20)

PDF
Pl python python w postgre-sql
Piotr Pałkiewicz
 
PDF
Phil Bartie QGIS PLPython
Ross McDonald
 
PDF
Imugi: Compiler made with Python
Han Lee
 
PDF
Adding replication protocol support for psycopg2
Alexander Shulgin
 
PDF
Getting Started with PL/Proxy
Peter Eisentraut
 
PDF
oodp elab.pdf
SWATIKUMARIRA2111030
 
PDF
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
SmartTools
 
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PgDay.Seoul
 
PDF
Bind me if you can
Ovidiu Farauanu
 
PDF
groovy databases
Paul King
 
PDF
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
 
PDF
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Yashpatel821746
 
PDF
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
PPTX
Kotlin / Android Update
Garth Gilmour
 
PPTX
Relational Database Access with Python
Mark Rees
 
ODP
Writing MySQL UDFs
Roland Bouman
 
PPTX
Add an interactive command line to your C++ application
Daniele Pallastrelli
 
PDF
Create a JAVA program that performs file IO and database interaction.pdf
malavshah9013
 
PDF
C++ manual Report Full
Thesis Scientist Private Limited
 
PDF
What's new in Python 3.11
Henry Schreiner
 
Pl python python w postgre-sql
Piotr Pałkiewicz
 
Phil Bartie QGIS PLPython
Ross McDonald
 
Imugi: Compiler made with Python
Han Lee
 
Adding replication protocol support for psycopg2
Alexander Shulgin
 
Getting Started with PL/Proxy
Peter Eisentraut
 
oodp elab.pdf
SWATIKUMARIRA2111030
 
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
SmartTools
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PgDay.Seoul
 
Bind me if you can
Ovidiu Farauanu
 
groovy databases
Paul King
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Kotlin / Android Update
Garth Gilmour
 
Relational Database Access with Python
Mark Rees
 
Writing MySQL UDFs
Roland Bouman
 
Add an interactive command line to your C++ application
Daniele Pallastrelli
 
Create a JAVA program that performs file IO and database interaction.pdf
malavshah9013
 
C++ manual Report Full
Thesis Scientist Private Limited
 
What's new in Python 3.11
Henry Schreiner
 

More from Henning Jacobs (20)

PDF
How Zalando runs Kubernetes clusters at scale on AWS - AWS re:Invent
Henning Jacobs
 
PDF
Open Source at Zalando - OSB Open Source Day 2019
Henning Jacobs
 
PDF
Why I love Kubernetes Failure Stories and you should too - GOTO Berlin
Henning Jacobs
 
PDF
Why Kubernetes? Cloud Native and Developer Experience at Zalando - Enterprise...
Henning Jacobs
 
PDF
Why Kubernetes? Cloud Native and Developer Experience at Zalando - OWL Tech &...
Henning Jacobs
 
PDF
Kubernetes + Python = ❤ - Cloud Native Prague
Henning Jacobs
 
PDF
Kubernetes Failure Stories, or: How to Crash Your Cluster - ContainerDays EU ...
Henning Jacobs
 
PDF
Why we don’t use the Term DevOps: the Journey to a Product Mindset - DevOpsCo...
Henning Jacobs
 
PDF
Why we don’t use the Term DevOps: the Journey to a Product Mindset - Destinat...
Henning Jacobs
 
PDF
Kubernetes Failure Stories - KubeCon Europe Barcelona
Henning Jacobs
 
PDF
Optimizing Kubernetes Resource Requests/Limits for Cost-Efficiency and Latenc...
Henning Jacobs
 
PDF
Developer Experience at Zalando - CNCF End User SIG-DX
Henning Jacobs
 
PDF
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Henning Jacobs
 
PDF
Let's talk about Failures with Kubernetes - Hamburg Meetup
Henning Jacobs
 
PDF
Developer Experience at Zalando - Handelsblatt Strategisches IT-Management 2019
Henning Jacobs
 
PDF
Running Kubernetes in Production: A Million Ways to Crash Your Cluster - DevO...
Henning Jacobs
 
PDF
Optimizing Kubernetes Resource Requests/Limits for Cost-Efficiency and Latenc...
Henning Jacobs
 
PDF
Running Kubernetes in Production: A Million Ways to Crash Your Cluster - Cont...
Henning Jacobs
 
PDF
API First with Connexion - PyConWeb 2018
Henning Jacobs
 
PDF
Developer Journey at Zalando - Idea to Production with Containers in the Clou...
Henning Jacobs
 
How Zalando runs Kubernetes clusters at scale on AWS - AWS re:Invent
Henning Jacobs
 
Open Source at Zalando - OSB Open Source Day 2019
Henning Jacobs
 
Why I love Kubernetes Failure Stories and you should too - GOTO Berlin
Henning Jacobs
 
Why Kubernetes? Cloud Native and Developer Experience at Zalando - Enterprise...
Henning Jacobs
 
Why Kubernetes? Cloud Native and Developer Experience at Zalando - OWL Tech &...
Henning Jacobs
 
Kubernetes + Python = ❤ - Cloud Native Prague
Henning Jacobs
 
Kubernetes Failure Stories, or: How to Crash Your Cluster - ContainerDays EU ...
Henning Jacobs
 
Why we don’t use the Term DevOps: the Journey to a Product Mindset - DevOpsCo...
Henning Jacobs
 
Why we don’t use the Term DevOps: the Journey to a Product Mindset - Destinat...
Henning Jacobs
 
Kubernetes Failure Stories - KubeCon Europe Barcelona
Henning Jacobs
 
Optimizing Kubernetes Resource Requests/Limits for Cost-Efficiency and Latenc...
Henning Jacobs
 
Developer Experience at Zalando - CNCF End User SIG-DX
Henning Jacobs
 
Ensuring Kubernetes Cost Efficiency across (many) Clusters - DevOps Gathering...
Henning Jacobs
 
Let's talk about Failures with Kubernetes - Hamburg Meetup
Henning Jacobs
 
Developer Experience at Zalando - Handelsblatt Strategisches IT-Management 2019
Henning Jacobs
 
Running Kubernetes in Production: A Million Ways to Crash Your Cluster - DevO...
Henning Jacobs
 
Optimizing Kubernetes Resource Requests/Limits for Cost-Efficiency and Latenc...
Henning Jacobs
 
Running Kubernetes in Production: A Million Ways to Crash Your Cluster - Cont...
Henning Jacobs
 
API First with Connexion - PyConWeb 2018
Henning Jacobs
 
Developer Journey at Zalando - Idea to Production with Containers in the Clou...
Henning Jacobs
 

Recently uploaded (20)

PDF
Software Development Company | KodekX
KodekX
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Software Development Company | KodekX
KodekX
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
This slide provides an overview Technology
mineshkharadi333
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Doc9.....................................
SofiaCollazos
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 

"PostgreSQL and Python" Lightning Talk @EuroPython2014

  • 2. Well known... import psycopg2 conn = psycopg2.connect('dbname=mydb user=myusr') cur = conn.cursor() cur.execute('SELECT * FROM mytable') row = cur.fetchone()
  • 3. PostgreSQL speaks Python! CREATE FUNCTION greet(name TEXT) RETURNS TEXT AS $$ import random attr = random.choice([ 'great', 'fabulous', 'bright', 'brave']) return 'Hello {} {}!'.format(attr, name) $$ LANGUAGE plpythonu; SELECT greet('Python fan');
  • 4. PostgreSQL speaks Python ● PL/Python – “just” another procedural language ● SQL script that contains a string of Python ● Allows using any Python module
  • 5. Real World Examples... mydb=# SELECT * FROM get_server_connection_stats() LIMIT 5; server_addr | server_port | client_addr | client_port ---------------+-------------+--------------+------------- 10.160.26.128 | 5435 | 10.111.74.16 | 40524 10.160.26.128 | 5435 | 10.160.26.1 | 49681 10.160.26.128 | 5435 | 10.160.26.1 | 15456 10.160.26.128 | 5435 | 10.160.26.2 | 64136 10.160.26.128 | 5435 | 10.160.26.1 | 1938 (5 rows)
  • 6. Expose Stats via SQL with Python CREATE FUNCTION get_server_connection_stats() RETURNS SETOF server_connection AS $$ # … try: with open('/proc/net/tcp') as fp: lines = fp.read().splitlines() except: plpy.error('Unable to read /proc/net/tcp') # … return result $$ LANGUAGE plpythonu;
  • 7. Custom JSON Type Validation CREATE TRIGGER validation AFTER INSERT OR UPDATE ON config_value FOR EACH … CREATE FUNCTION validate(value JSON, type_id INT) RETURNS VOID AS $$ import json # … complex type checking here # … using all of Python's expressiveness :-) raise ValueError('{} not valid'.format(value)) $$ LANGUAGE plpythonu;