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

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
 

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
Business logic with PostgreSQL and Python
Hubert Piotrowski
 
PPTX
Getting started with postgresql
botsplash.com
 
PPTX
Relational Database Access with Python
Mark Rees
 
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
obdlioubysz
 
PDF
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
kapuilakna
 
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
mavriclasme
 
PDF
Migrating to postgresql
botsplash.com
 
PDF
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
raiyaalaiaya
 
PDF
Learning postgresql
DAVID RAUDALES
 
PPTX
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Dinesh Neupane
 
PDF
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
trddarvai
 
PDF
PostgreSQL - Case Study
S.Shayan Daneshvar
 
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
bhaveeranirh
 
PDF
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
servanjervy
 
PDF
Pl/Python
Command Prompt., Inc
 
PDF
Володимир Гоцик. Getting maximum of python, django with postgres 9.4. PyCon B...
Alina Dolgikh
 
PDF
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Citus Data
 
PPTX
Postgresql
NexThoughts Technologies
 
PDF
Securing PostgreSQL from External Attack
All Things Open
 
PDF
Plpgsql russia-pgconf
Pavel Stěhule
 
Business logic with PostgreSQL and Python
Hubert Piotrowski
 
Getting started with postgresql
botsplash.com
 
Relational Database Access with Python
Mark Rees
 
PostgreSQL Server Programming 2nd Edition Usama Dar
obdlioubysz
 
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
kapuilakna
 
PostgreSQL Server Programming 2nd Edition Usama Dar
mavriclasme
 
Migrating to postgresql
botsplash.com
 
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
raiyaalaiaya
 
Learning postgresql
DAVID RAUDALES
 
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Dinesh Neupane
 
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
trddarvai
 
PostgreSQL - Case Study
S.Shayan Daneshvar
 
PostgreSQL Server Programming 2nd Edition Usama Dar
bhaveeranirh
 
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
servanjervy
 
Володимир Гоцик. Getting maximum of python, django with postgres 9.4. PyCon B...
Alina Dolgikh
 
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Citus Data
 
Securing PostgreSQL from External Attack
All Things Open
 
Plpgsql russia-pgconf
Pavel Stěhule
 
Ad

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
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 

"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;