Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres Case Sensitivity

I have imported 100 of tables in Postgres from MSSql server 2008 through tool which created all the tables along with their columns in capital letter. Now if I want to make a data view from table e.g - STD_TYPE_CODES as-

select * from STD_TYPE_CODES 

I am getting following error-

ERROR:  relation "std_type_codes" does not exist LINE 1: select * from STD_TYPE_CODES                   ^ ********** Error ********** ERROR: relation "std_type_codes" does not exist SQL state: 42P01 Character: 15 

I know I can put the quotes around the table name as-

select * from "STD_TYPE_CODES" 

But as I have worked with MSSql Server, there is no such kind of issue. So is there any way to get rid of this? Please help.

like image 735
Viks Avatar asked Feb 15 '14 10:02

Viks


People also ask

How do I make a case-insensitive in PostgreSQL?

While using regular expressions, we need to use the PostgreSQL ~* operator instead of the like operator; we can also use the ilike operator in PostgreSQL. We can also create an extension name as citext to use the case insensitive query in PostgreSQL; we need to create it first to use the extension of citext.

Is Postgres VARCHAR case-sensitive?

Typically a VARCHAR field is treated as case-sensitive in Postgres. Meaning that fieldname = 'FOO' and fieldname = 'foo' won't find any matches when the actual value is "FOO".

Is Pgadmin case-sensitive?

PostgreSQL, unlike MySQL, treats strings as case sensitive in all circumstances.

Is Aurora DB case-sensitive?

Aurora MySQL names are case sensitive and can be adjusted based on the parameter mentioned following. In Aurora MySQL, the case sensitivity is determined by the lower_case_table_names parameter value.


1 Answers

In PostgreSQL unquoted names are case-insensitive. Thus SELECT * FROM hello and SELECT * FROM HELLO are equivalent.

However, quoted names are case-sensitive. SELECT * FROM "hello" is not equivalent to SELECT * FROM "HELLO".

To make a "bridge" between quoted names and unquoted names, unquoted names are implicitly lowercased, thus hello, HELLO and HeLLo are equivalent to "hello", but not to "HELLO" or "HeLLo" (OOPS!).

Thus, when creating entities (tables, views, procedures, etc) in PostgreSQL, you should specify them either unquoted, or quoted-but-lowercased.


To convert existing tables/views/etc you can use something like ALTER TABLE "FOO" RENAME TO "foo".

Or, try to modify dump from MSSQL to make it "PostgreSQL-compatible" (so that it will contain foos or "foo"s but not "FOO"s).

  • Either by explicitly editing dump file. (If you're using Linux, you can do sed -r 's/"[^"]+"/\L\0/g' dumpfile — however be warned that this command may also modify text in string literals.)
  • Or by specifying some options when getting dump from MSSQL. (I'm not sure if there are such options in MSSQL, never used it, but probably such options should exist.)
like image 87
3 revs, 3 users 90% Avatar answered Sep 24 '22 15:09

3 revs, 3 users 90%