backslash_quote
Read PG 18 manual ↗This controls whether a quote mark can be represented by \' in a string literal.
- Type
- enum
- Context
- user
- Measured default
- safe_encoding
- Unit
- —
- Metadata snapshot
- 18
- Allowed values
- safe_encoding, on, off
Definition PG 18 manual
This controls whether a quote mark can be represented by \' in a string literal. The preferred, SQL-standard way to represent a quote mark is by doubling it ('') but PostgreSQL has historically also accepted \'. However, use of \' creates security risks because in some client character set encodings, there are multibyte characters in which the last byte is numerically equivalent to ASCII \. If client-side code does escaping incorrectly then an SQL-injection attack is possible. This risk can be prevented by making the server reject queries in which a quote mark appears to be escaped by a backslash. The allowed values of backslash_quote are on (allow \' always), off (reject always), and safe_encoding (allow only if client encoding does not allow ASCII \ within a multibyte character). safe_encoding is the default setting.
Note that in a standard-conforming string literal, \ just means \ anyway. This parameter only affects the handling of non-standard-conforming literals, including escape string syntax (E'...').
Measured default history
| Version interval | Default |
|---|---|
| 9.0 – 19 | safe_encoding |
Authored guidance from the GUC source snapshot; the version-specific manual above is the definition reference. View source ↗
How it works
Sets whether "\'" is allowed in string literals. It can be changed at session scope, so different sessions may observe different behavior.
The safe_encoding mode accepts \' only when the client encoding cannot contain a backslash byte inside a multibyte character. This defense belongs to old string-literal syntax; E'...' is the explicit escape-string form and standard_conforming_strings governs ordinary strings.
Monitor and change backslash_quote together with array_nulls, escape_string_warning, standard_conforming_strings. Validate on the relevant server role and real workload, then use its user context to choose session change, reload, or restart; a historical boot default is not the current effective value.
Operational considerations
Keeping a compatibility switch permanently instead of fixing the client.
Testing in one session and deploying globally to unrelated applications.
Confusing parsing compatibility with data or security compatibility.
Forgetting to remove an override after the upgrade migration is complete.
Workload guidance
OLAP: Regression-test ETL, generated SQL, and old drivers, where parsing/quoting assumptions hide. Performance is rarely a reason to change this switch.
OLTP: Keep the modern default and repair legacy clients/SQL that depend on backslash_quote. Test migration at session scope first; do not make a compatibility switch permanent cluster policy.
SMALL: Keep the default without a legacy requirement. If temporarily enabled, record owner, affected connections, and a removal date.
Version history 2
- PG 18 → 19changed
- PG 13 → 14changed
Related entries
Further reading
Definition snapshot: english-manuals:c504c4df6529264440b9ba30dde… · English manual source