Are table names case sensitive in Heroku Postgres add-on? [duplicate] - postgresql

I have a db table say, persons in Postgres handed down by another team that has a column name say, "first_Name". Now am trying to use PG commander to query this table on this column-name.
select * from persons where first_Name="xyz";
And it just returns
ERROR: column "first_Name" does not exist
Not sure if I am doing something silly or is there a workaround to this problem that I am missing?

Identifiers (including column names) that are not double-quoted are folded to lowercase in PostgreSQL. Column names that were created with double-quotes and thereby retained uppercase letters (and/or other syntax violations) have to be double-quoted for the rest of their life:
"first_Name"
Values (string literals / constants) are enclosed in single quotes:
'xyz'
So, yes, PostgreSQL column names are case-sensitive (when double-quoted):
SELECT * FROM persons WHERE "first_Name" = 'xyz';
Read the manual on identifiers here.
My standing advice is to use legal, lower-case names exclusively so double-quoting is never required.

To quote the documentation:
Key words and unquoted identifiers are case insensitive. Therefore:
UPDATE MY_TABLE SET A = 5;
can equivalently be written as:
uPDaTE my_TabLE SeT a = 5;
You could also write it using quoted identifiers:
UPDATE "my_table" SET "a" = 5;
Quoting an identifier makes it case-sensitive, whereas unquoted names are always folded to lower case (unlike the SQL standard where unquoted names are folded to upper case). For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.
If you want to write portable applications you are advised to always quote a particular name or never quote it.

The column names which are mixed case or uppercase have to be double quoted in PostgresQL. So best convention will be to follow all small case with underscore.

if use JPA I recommend change to lowercase schema, table and column names, you can use next intructions for help you:
select
psat.schemaname,
psat.relname,
pa.attname,
psat.relid
from
pg_catalog.pg_stat_all_tables psat,
pg_catalog.pg_attribute pa
where
psat.relid = pa.attrelid
change schema name:
ALTER SCHEMA "XXXXX" RENAME TO xxxxx;
change table names:
ALTER TABLE xxxxx."AAAAA" RENAME TO aaaaa;
change column names:
ALTER TABLE xxxxx.aaaaa RENAME COLUMN "CCCCC" TO ccccc;

You can try this example for table and column naming in capital letters. (postgresql)
//Sql;
create table "Test"
(
"ID" integer,
"NAME" varchar(255)
)
//C#
string sqlCommand = $#"create table ""TestTable"" (
""ID"" integer GENERATED BY DEFAULT AS IDENTITY primary key,
""ExampleProperty"" boolean,
""ColumnName"" varchar(255))";

Related

changing a columns name that starts with a numeric value

I am attempting to change a column name but there is an issue because my original column name has a number.
Here is what my table columns looks like
name price small medium large xl 2xl 3xl
When i do the following
ALTER TABLE tableName
RENAME small TO abc;
It executes well.
However when i do the following
ALTER TABLE tableName
RENAME 2xl TO xxl;
I get error saying syntax error at or near "2"
Does this mean i can never change this column's name because it starts with a numeric value?
Any time a column name begins with a non-alpha character, or contains special characters (spaces, etc) or is a keyword like "from," (but don't do that), you have to put the column name in quotes:
alter table tableName rename "2xl" to xxl;
As an aside, it's generally advisable to avoid object names that require double quotes. It's just more work in everything else. It's not wrong precisely speaking, just more work.
Enclose the identifier with the special characters in double quotes.
ALTER TABLE tablename
RENAME "2xl" TO xxl;

Why PostgreSQL does not like UPPERCASE table names?

I have recently tried to create some tables in PostgreSQL all in uppercase names. However in order to query them I need to put the table name inside the quotation "TABLE_NAME". Is there any way to avoid this and tell the postgres to work with uppercase name as normal ?
UPDATE
this query create a table with lowercase table_name
create table TABLE_NAME
(
id integer,
name varchar(255)
)
However, this query creates a table with uppercase name "TABLE_NAME"
create table "TABLE_NAME"
(
id integer,
name varchar(255)
)
the problem is the quotations are part of the name now!!
in my case I do not create the tables manually, another Application creates the table and the names are in capital letters. this cause problems when I want to use CQL filters via Geoserver.
put table name into double quotes if you want postgres to preserve case for relation names.
Quoting an identifier also makes it case-sensitive, whereas unquoted
names are always folded to lower case. For example, the identifiers
FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo"
and "FOO" are different from these three and each other. (The folding
of unquoted names to lower case in PostgreSQL is incompatible with the
SQL standard, which says that unquoted names should be folded to upper
case. Thus, foo should be equivalent to "FOO" not "foo" according to
the standard. If you want to write portable applications you are
advised to always quote a particular name or never quote it.)
from docs (emphasis mine)
example with quoting:
t=# create table "UC_TNAME" (i int);
CREATE TABLE
t=# \dt+ UC
t=# \dt+ "UC_TNAME"
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------+-------+----------+---------+-------------
public | UC_TNAME | table | postgres | 0 bytes |
(1 row)
example without quoting:
t=# create table UC_TNAME (i int);
CREATE TABLE
t=# \dt+ UC_TNAME
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------+-------+----------+---------+-------------
public | uc_tname | table | postgres | 0 bytes |
(1 row)
So if you created table with quotes, you should not skip quotes querying it. But if you skipped quotes creating object, the name was folded to lowercase and so will be with uppercase name in query - this way you "won't notice" it.
The question implies that double quotes, when used to force PostgreSQL to recognize casing for an identifier name, actually become part of the identifier name. That's not correct. What does happen is that if you use double quotes to force casing, then you must always use double quotes to reference that identifier.
Background:
In PostgreSQL, names of identifiers are always folded to lowercase unless you surround the identifier name with double quotes. This can lead to confusion.
Consider what happens if you run these two statements in sequence:
CREATE TABLE my_table (
t_id serial,
some_value text
);
That creates a table named my_table.
Now, try to run this:
CREATE TABLE My_Table (
t_id serial,
some_value text
);
PostgreSQL ignores the uppercasing (because the table name is not surrounded by quotes) and tries to make another table called my_table. When that happens, it throws an error:
ERROR: relation "my_table" already exists
To make a table with uppercase letters, you'd have to run:
CREATE TABLE "My_Table" (
t_id serial,
some_value text
);
Now you have two tables in your database:
Schema | Name | Type | Owner
--------+---------------------------+-------+----------
public | My_Table | table | postgres
public | my_table | table | postgres
The only way to ever access My_Table is to then surround the identifier name with double quotes, as in:
SELECT * FROM "My_Table"
If you leave the identifier unquoted, then PostgreSQL would fold it to lowercase and query my_table.
In simple words, Postgres treats the data in (double-quotes) "" as case-sensitive. And remaining as lowercase.
Example: we can create 2-columns with names DETAILS and details and while querying:
select "DETAILS"
return DETAILS column data and
select details/DETAILS/Details/"details"
returns details column data.

pandas read_sql convers column names to lower case - is there a workaroud?

related: pandas read_sql drops dot in column names
I use pandas.read_sql to create a data frame from an sql query from a postgres database.
some column aliases\names use mixed case, and I want it to propagate to the data frame.
however, pandas (or the underlining engine - SQLAlchemy as much as I know) return only lower case field names.
is there a workaround?
(besides using a lookup table and fix the values afterwards)
Postgres normalizes unquoted column names to lower case. If you have such a table:
create table foo ("Id" integer, "PointInTime" timestamp);
PostgreSQL will obey the case, but you will have to specify table names quoted as such:
select "Id", "PointInTime" from foo;
A better solution is to add column aliases, eg:
select name as "Name", value as "Value" from parameters;
And Postgres will return properly cased column names. If the problem is SQLAlchemy or pandas, then this will not suffice.

INSERT with alias name is not working on PostgreSQL

SQL Query on PostgreSQL:
insert into TOKEPOOLAMT (TOKEPOOLAMT.TOKEPOOLAMTID,TOKEPOOLAMT.TOKERULEID)
values (151, 176);
Giving error:
com.edb.util.PSQLException:
ERROR: column "tokepoolamt" of relation "tokepoolamt" does not exist
But:
insert into TOKEPOOLAMT (TOKEPOOLAMTID,TOKERULEID) values (151, 176);
is working fine.
Can anybody explain why alias name with column in insert statement not working?
There are no aliases involved here. Your error is that column names in the column list of an INSERT command cannot be table-qualified. #pozs already provided the fitting quote from the manual in his comment.
I don't think it's an issue of case. I tried with both the cases.
That's missing the point. In Postgres, identifiers are folded to lower case unless double-quoted. If you double-quoted a name at creation time you preserved a case sensitive spelling and need to double-quote for the rest of the object's life - unless it was a legal, lower-case name to begin with, then quoting won't make a difference. Details:
Are PostgreSQL column names case-sensitive?

The relation does not exist in postgresql

I am have big problem with Postgresql.
I'm trying to insert record to postgres.
But i had error:
The relation "member_orderinfo2" does not exist. But this table had exist when i'm insert record.
This is my query to insert
insert into "member_orderinfo2"(order_id,slim_code,order_date,customer_order_number,ship_date,deliver_company,deliver_number,deliver_address,product_code,product_name,amount,unit_price,total_cost,order_customer_name,ship_status) values
( '30059078','4170906','20131118','15754727-03直送','0','メーカー直送','','京都府綾部市味方町1京セラコミュニケーションシステム','0996381','RD151-50SN(ジユウリヨウダ','5','5120.00','25600','梶野様','入荷待' )
What is error ???
It looks like case sensitivity issue.
You used a double quotes for table name - "member_orderinfo2" - it means so table have to be named exactly member_orderinfo2 with only lower chars. Double quotes are used for case sensitive identifiers. Usually better don't use it when it is possible - are necessary when used identifier is keyword - so don't use case sensitive identifiers and don't use keywords as identifiers.