HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Quoting columns with spaces in PostgreSQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlquotingcolumnswithspaces

Problem

I migrated a database from SQL Server to PostgreSQL.

Most column names contain double words, for example:

SELECT [Column Name] FROM table;


...which does not work in PostgreSQL.

What is the correct syntax for PostgreSQL?

Solution

In most RDBMSs, double-quotes are what are used to specify an exact spelling of something.. (single quotes being string delimiters).

SELECT
  tab."This IS My Column EXACTLY" AS col
FROM "My TabLE Name Contains Spaces Too!" tab
WHERE tab."ANOTHER UGLY COLUMN name" = 'MyFilterString';


Notice that capital/lowercase also matters when using double-quotes. (Postgres lower-cases everything when double-quotes are not used ... Oracle upper-cases everthing, etc..)

SELECT COLUMN1 FROM TABLE


in postgres, is different from

SELECT "COLUMN1" FROM TABLE


where as in oracle, is different from

SELECT "column1" FROM TABLE

Code Snippets

SELECT
  tab."This IS My Column EXACTLY" AS col
FROM "My TabLE Name Contains Spaces Too!" tab
WHERE tab."ANOTHER UGLY COLUMN name" = 'MyFilterString';
SELECT COLUMN1 FROM TABLE
SELECT "COLUMN1" FROM TABLE
SELECT "column1" FROM TABLE

Context

StackExchange Database Administrators Q#118059, answer score: 30

Revisions (0)

No revisions yet.