Skip to Content
Mastering PostgreSQL 12 - Third Edition
book

Mastering PostgreSQL 12 - Third Edition

by Hans-Jürgen Schönig
November 2019
Beginner to intermediate
470 pages
11h 59m
English
Packt Publishing
Content preview from Mastering PostgreSQL 12 - Third Edition

Handling quoting and the string format

One of the most important things in database programming is quoting. If you don't use proper quoting, you will surely get into trouble with SQL injection and open unacceptable security holes.

What is SQL injection? Let's consider the following example:

CREATE FUNCTION broken(text) RETURNS void AS 
$$  
DECLARE 
  v_sql text; 
BEGIN 
  v_sql := 'SELECT schemaname 
            FROM pg_tables 
            WHERE tablename = ''' || $1 || ''''; 
  RAISE NOTICE 'v_sql: %', v_sql; 
  RETURN; 
END; 
$$ LANGUAGE 'plpgsql'; 

In this example, the SQL code is simply pasted together without ever worrying about security. All we are doing here is using the || operator to concatenate strings. This works fine if people run normal queries. Consider the following ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering PostgreSQL 11 - Second Edition

Mastering PostgreSQL 11 - Second Edition

Hans-Jürgen Schönig
Learning PostgreSQL 11 - Third Edition

Learning PostgreSQL 11 - Third Edition

Christopher Travers, Andrey Volkov
PostgreSQL Server Programming - Second Edition

PostgreSQL Server Programming - Second Edition

Usama Dar, Hannu Krosing, Jim Mlodgenski, Kirk Roybal

Publisher Resources

ISBN: 9781838988821Supplemental Content