Re: Is there a way to check if objects are created with quotes or not?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 21, 2025 at 3:28 PM Edwin UY <edwin.uy@xxxxxxxxx> wrote:
>
> I am just now finding out that I sometimes I have to enclosed tables in
> quotes, that is a name named table1 need to be quoted as 'table1' otherwise it
> gives error.

I'm guessing you mean quoting with double quotes, like "table1". Object names,
a.k.a identifiers, may be quoted with double quotes; single quotes are used for
text and other literals/constants.

The link shared by Tom [1] has the complete set of rules around quoting, but in
short, the general rule is that if an identifier is not qupted by the
user/application, it will be converted to lower-case. If the identifier is
quoted by the user/application, then Postgres will retain the case used in the
name.

For example, first two of the following queries are equivalent, whereas the
third one refers to a different object than the first two.

select * from table1;
select * from "table1";
select * from "Table1";

> Is there any way to find out what are those such tables so we can rename it to
> be without the quotes? Or maybe it is not a good idea to rename as it might
> break the indexes / foreign keys / sequences?

Renaming an object does _not_ break any dependencies like indexes, etc. So it's
safe to rename an object. But this may break your application queries, but it
appears that you're willing to accept that and modify the application queries.

Here's an quick and dirty example that shows how to find table names with
upper-case characters; converting them to lower case should help you. This may
not be sufficient, so be sure to read through the documnetation shared by Tom to
take care of unusual cases.

postgres=# create table table1();
CREATE TABLE
postgres=# create table "Table1"();
CREATE TABLE
postgres=# select relname from pg_class where relname <> lower(relname);
 relname
---------
 Table1
(1 row)

[1]: Identifiers and Key Words
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Best regards,
Gurjeet
http://Gurje.et






[Index of Archives]     [Postgresql Home]     [Postgresql General]     [Postgresql Performance]     [Postgresql PHP]     [Postgresql Jobs]     [PHP Users]     [PHP Databases]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Yosemite Forum]

  Powered by Linux