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
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
PDF
Programming with Python and PostgreSQL
PDF
はじめてのGroovy
PDF
Elm: give it a try
PDF
Cycle.js: Functional and Reactive
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
PPTX
ES6 in Real Life
Python postgre sql a wonderful wedding
Psycopg2 - Connect to PostgreSQL using Python Script
Programming with Python and PostgreSQL
はじめてのGroovy
Elm: give it a try
Cycle.js: Functional and Reactive
The Ring programming language version 1.5.3 book - Part 25 of 184
ES6 in Real Life

What's hot (18)

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

Viewers also liked (18)

PDF
PostgreSQL - C言語によるユーザ定義関数の作り方
PDF
Www Kitebird Com Articles Pydbapi Html Toc 1
ODP
Rethink db with Python
PPTX
Succumbing to the Python in Financial Markets
PDF
MySQL User Conference 2009: Python and MySQL
PDF
Scaling mysql with python (and Docker).
PPTX
Relational Database Access with Python ‘sans’ ORM
PDF
Python for Derivative Analytics
PDF
Python Utilities for Managing MySQL Databases
PDF
PostgreSQLとPythonとSQL
PPTX
Python for Big Data Analytics
PPTX
Python for Big Data Analytics
PDF
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
PPTX
Python PPT
PDF
Orchestrating Docker containers at scale
ODP
Python Presentation
PPT
Introduction to Python
PostgreSQL - C言語によるユーザ定義関数の作り方
Www Kitebird Com Articles Pydbapi Html Toc 1
Rethink db with Python
Succumbing to the Python in Financial Markets
MySQL User Conference 2009: Python and MySQL
Scaling mysql with python (and Docker).
Relational Database Access with Python ‘sans’ ORM
Python for Derivative Analytics
Python Utilities for Managing MySQL Databases
PostgreSQLとPythonとSQL
Python for Big Data Analytics
Python for Big Data Analytics
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Python PPT
Orchestrating Docker containers at scale
Python Presentation
Introduction to Python
Ad

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

PDF
Business logic with PostgreSQL and Python
PPTX
Getting started with postgresql
PPTX
Relational Database Access with Python
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
PDF
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
PDF
PostgreSQL Server Programming - Second Edition Dar
PDF
Migrating to postgresql
PDF
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
PDF
Learning postgresql
PPTX
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
PDF
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
PDF
PostgreSQL - Case Study
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
PDF
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
PDF
PDF
Володимир Гоцик. Getting maximum of python, django with postgres 9.4. PyCon B...
PDF
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
PPTX
PDF
Securing PostgreSQL from External Attack
Business logic with PostgreSQL and Python
Getting started with postgresql
Relational Database Access with Python
PostgreSQL Server Programming 2nd Edition Usama Dar
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
PostgreSQL Server Programming 2nd Edition Usama Dar
PostgreSQL Server Programming - Second Edition Dar
Migrating to postgresql
Get PostgreSQL Server Programming - Second Edition Dar free all chapters
Learning postgresql
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
PostgreSQL - Case Study
PostgreSQL Server Programming 2nd Edition Usama Dar
PostgreSQL Server Programming Second Edition Usama Dar Hannu Krosing Jim Mlod...
Володимир Гоцик. Getting maximum of python, django with postgres 9.4. PyCon B...
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Securing PostgreSQL from External Attack

More from Henning Jacobs (20)

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

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

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