Join our Newsletter — 33% off our NHI Course
Home› Glossary› Foundations & NHI Taxonomy› Dollar-Quoted String
Foundations & NHI Taxonomy

Dollar-Quoted String

← Back to Glossary
By NHI Mgmt Group Updated November 25, 2026 Domain: Foundations & NHI Taxonomy

A dollar-quoted string is a PostgreSQL string literal enclosed in matching dollar tags instead of single quotes. It is used for text that contains many quotes or backslashes, because the contents are taken literally until the closing tag appears.

How Dollar-Quoted Strings Work

Dollar-quoted strings are a PostgreSQL-specific literal format that changes how the parser treats the enclosed text. Instead of escaping every single quote or backslash, you wrap the content in matching dollar tags, and PostgreSQL reads everything literally until it finds the closing tag. That makes complex SQL text easier to write, read, and maintain.

The key idea is delimiter-based parsing rather than character-by-character escaping. Because the opening and closing tags must match exactly, the string can safely contain apostrophes, quotation marks, and many backslashes without the usual quote-doubling overhead. This is especially useful when embedding SQL, regular expressions, or large blocks of text inside another statement.

Why They Are Useful in PostgreSQL

Dollar-quoted strings reduce the friction that comes with conventional single-quoted literals. They are often used in stored procedures, function bodies, dynamic SQL, and migration scripts, where nested quoting would otherwise become hard to follow. The format helps keep the intent visible, especially when a string contains multiple layers of syntax.

They also make it easier to copy or generate code that already contains quoted text. With single-quoted literals, every apostrophe inside the content must be escaped, which can make long statements fragile and visually noisy. Dollar quoting removes that burden while preserving the exact text between the tags.

Because the contents are taken literally, the chosen tag becomes part of the syntax boundary. That means the only real parsing risk is picking a delimiter that accidentally appears inside the payload. In practice, well-chosen tags make the format robust and predictable for long or nested text blocks.

Common Syntax Pitfalls

The main source of errors is mismatch, not escaping. If the opening and closing dollar tags differ by even one character, PostgreSQL will not terminate the string where you expect. A second issue is choosing a tag that appears in the content itself, which can end the literal early and produce a syntax error or truncated statement.

Another common mistake is assuming dollar quoting is a universal SQL feature. It is strongly associated with PostgreSQL and its procedural language support, so code that uses it may not be portable to other database engines without changes. For teams that work across platforms, that portability difference matters.

Developers also need to remember that dollar quoting changes how the parser reads the literal, not what the database ultimately stores. The resulting value is still just text. The benefit is cleaner syntax at authoring time, not a different data type or special runtime behaviour.

Where Dollar-Quoted Strings Fit in SQL Security and Maintainability

Dollar-quoted strings are primarily a parsing and maintainability feature, but they still matter to secure development because easier-to-read SQL is easier to review. When complex code is buried inside repeated quote escapes, reviewers are more likely to miss logic errors or unsafe concatenation patterns. Cleaner literal handling supports more reliable code inspection.

They are also useful when defining database-side logic that must embed text safely and clearly. Even so, dollar quoting is not a substitute for parameterised queries, input validation, or safe query construction. It solves literal syntax problems, not SQL injection by itself.

For that reason, the best way to think about dollar-quoted strings is as a readability and correctness aid. They help PostgreSQL accept complex text exactly as written, but the surrounding application still needs strong query discipline, especially when user-controlled values are involved.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

NIST SP 800-53 Rev 5, OWASP ASVS and CIS Controls v8 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST SP 800-53 Rev 5SI-10 — Information Input ValidationDollar-quoted SQL still needs safe handling of untrusted input.
Recommendation — Validate external input before composing SQL that uses literal text.
OWASP ASVSV15 — Secure Coding and ArchitectureSafe literal handling is part of secure database-facing code design.
Recommendation — Use secure coding patterns so SQL literals do not become injection paths.
CIS Controls v8CIS-16 — Application Software SecurityApplication code that builds database statements must be developed and reviewed securely.
Recommendation — Review database statement construction for unsafe string handling.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org