PostgreSQL quote_ident() Function

The PostgreSQL quote_ident() function returns the given string appropriately quoted for using as an identifier in SQL statement strings.

Double quotes should only be added when necessary, such as if the string contains characters that are not identifiers or if the string is to keep uppercase letters, etc.

If the string contains double quotes, it will be converted to two double quotes. For example a"b to "a""b".

quote_ident() Syntax

This is the syntax of the PostgreSQL quote_ident() function:

quote_ident(string)

Parameters

string

Required. The string to be used as an identifier.

Return value

The PostgreSQL quote_ident() function returns a string that is properly quoted to be used as an identifier in SQL statement strings.

quote_ident() Examples

Basic usage

This example demonstrates how to use the quote_ident() function to convert an unqualified identifier to a qualified one.

SELECT
    quote_ident('hello world') AS "hello world",
    quote_ident('Hello-world') AS "Hello-world",
    quote_ident('helloWorld') AS "helloWorld",
    quote_ident('HelloWorld') AS "HelloWorld";
  hello world  |  Hello-world  |  helloWorld  |  HelloWorld
---------------+---------------+--------------+--------------
 "hello world" | "Hello-world" | "helloWorld" | "HelloWorld"

Here, the string hello world is not a qualified identifier, because it contains spaces. The result of the quote_ident() function ("hello world") is a qualified identifier that can be used in SQL statements.

Quote suitably

Double quotes are only added when necessary. If the parameter is already a qualified identifier, no quotes will be added. For example:

SELECT
    quote_ident('hello') AS "hello",
    quote_ident('world') AS "world",
    quote_ident('hello_world') AS "hello_world",
    quote_ident('hello world') AS "hello world";
 hello | world | hello_world |  hello world
-------+-------+-------------+---------------
 hello | world | hello_world | "hello world"

Here, only hello world is a unqualified identifier, so it is enclosed in double quotes.

Embedded quotes

If the string contains double quotes, it will be converted to two double quotes. For example a"b will be convert to "a""b".

SELECT quote_ident('Hello"World"');
   quote_ident
------------------
 "Hello""World"""