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
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 

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