# Postgres Dollar Quoting

DevFeed: [Postgres Dollar Quoting](<https://devfeed.tech/articles/postgres-dollar-quoting-41147.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2013/08/02/Postgres-Dollar-Quoting/>)

Author: Map

Published: 2013-08-02T20:55:56Z

Content type: tutorial

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [SQL](<https://devfeed.tech/topics/sql.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [Security](<https://devfeed.tech/topics/security.md>)

Tags: [database](<https://devfeed.tech/tags/database.md>), [postgres](<https://devfeed.tech/tags/postgres.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [security](<https://devfeed.tech/tags/security.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

This tutorial explains PostgreSQL dollar quoting, a way to enter string and data literals without escaping embedded apostrophes. It covers tagged and nested dollar quotes, examples in comments and JSON values, and warns that dollar quoting does not prevent SQL injection; parameter binding should be used for security.

## Source excerpt

After my most recent post on documenting your database I had a colleague and friend chime in: {% blockquote @danfarina https://twitter.com/danfarina/status/362007008079126528 %} @craigkerstiens You may want to mention for another post the generality of dollar quoting: it's not just for CREATE FUNCTION. {% endblockquote %} Luckily I was able to convince him to create the post. You can read a bit more on him below, but without further adieu here's a bit on dollar quoting within Postgres: Postgres supports two forms of entry of data literals into the system. One is the familiar single-quote: => SELECT 'hello'; ?column? ---------- hello (1 row) This format is problematic when one might be using single quotes in the textual string. Postgres also supports another way to enter data literals, most often seen in CREATE FUNCTION, but can be profitably used anywhere. This is called "dollar quoting," and it looks like this: => SELECT $$hello's the name of the game$$; ?column? ------------------------------ hello's the name of the game (1 row) If one needs nested dollar quoting, one can specify a string, much like the 'heredoc' feature seen in some programming languages: => SELECT $goodbye$hello's the name of the $$ game$goodbye$; ?column? --------------------------------- hello's the name of the $$ game (1 row) This can appear anywhere where single quotes would otherwise be, simplifying tasks like using contractions in database object comments, for example: => CREATE TABLE described(a int); => COMMENT ON TABLE described IS $$I'm describing this, including newlines and an apostrophe in the contraction "I'm."$$; Or, alternatively, entry of literals for types that may include apostrophes in their serialization, such as 'text' or 'json': => CREATE TABLE json(data json); => INSERT INTO json(data) VALUES ($${"quotation": "'there is no time like the present'"}$$); Security Even though dollar quotes can be used to reduce the pain of many quoting problems, don't be tempted to use them t