Alpha
Alpha

Reputation: 14026

How to make sql query on table name with space separated in Postgres?

I'm using postgres database and it has table with name say 'System Tenant'. Now I want to make query on it, I do -

select * from "System Tenant";

but it results into error -

ERROR:  relation "System Tenant" does not exist
LINE 1: select * from "System Tenant"
                       ^

Could you please suggest how I can resolve it?

Upvotes: 3

Views: 4976

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51406

lets say:

so=# create schema t;
CREATE SCHEMA
so=# create table t."Bad Name"();
CREATE TABLE
so=# create table "b@d Name"();
CREATE TABLE

now find all:

so=# select oid::regclass from pg_class where relname ilike '% name%';
     oid
--------------
 t."Bad Name"
 "b@d Name"
(2 rows)

and use exactly as it is listed:

so=# select * from t."Bad Name";
--
(0 rows)

or

so=# select * from "b@d Name";
--
(0 rows)

Upvotes: 3

Related Questions