PG_ESCAPE_STRING(n)

πŸ“¦ version 3.1.1, in alpine 3.22, alpine 3.23, alpine 3.24, nixpkgs unstable

πŸ’… PG_ESCAPE_STRING() πŸ’…

okay bestie, welcome to the PostgreSQL Tcl Interface Documentation page for pg_escape_string β€” she's here to keep your SQL from going feral. πŸ¦‹


🌸 NAME 🌸

pg_escape_string β€” escapes a string for inclusion into SQL statements. It's literally the same as pg_quote; it was added for consistency. no cap ✨

✨ SYNOPSIS ✨

pg_escape_string string

πŸ¦‹ DESCRIPTION πŸ¦‹

pg_escape_string quotes a string and escapes single quotes and backslashes within the string, making it safe for inclusion into SQL statements. it's giving "safety but make it cute" πŸ’…

If you're doing something like:

pg_exec $conn "insert into foo values ('$name');"

and name contains text including an unescaped single quote β€” like Bob's House β€” then at best the insert will fail, and at worst your software will be exploited via an SQL injection attack. 😭 (not the vibe, bestie.)

Passing value strings through pg_escape_string will properly quote them for insertion into SQL commands. Like:

pg_exec $conn "insert into foo values ([pg_escape_string $name]);"

...will make sure that any special characters that occur in name, such as a single quote or a backslash, will be properly quoted. periodt. πŸ”₯

⚠️ real talk: that SQL injection scenario is why escaping matters. escape your strings before they end up in the group chat. πŸ’€

πŸ“ ARGUMENTS πŸ“

  • string β€” the string to be escaped, bestie. πŸ“

πŸŽ€ RETURN VALUE πŸŽ€

Returns the string, escaped for inclusion into SQL queries. Note that it also adds a set of single quotes around the outside of the string as well β€” so yes, she's doing the absolute most, and that's on purpose. πŸ’–

🌈 SEE ALSO 🌈

In most cases, with recent versions of SQL, it is better to use the native parameter insertion capabilities of the SQL server and protocol. 🫢

If you are using a version of PostgreSQL more recent than 7.4, consider the optional parameter arguments to pg_exec and pg_sendquery, and the paramarray option to pg_exec, pg_sendquery, and pg_select. iykyk 🌟


xoxo bestie πŸ’– β€” source/version: PostgreSQL Tcl Interface Documentation, dated 17 September 2020. bye 🫢

πŸ“¦ where this page comes from