Creating and accessing a schema beginning with a numeric value in Postgresql - postgresql

I generate a 10-character alphanumeric random string as a schema name for each new tenant in my DB.
In this one instance, it generated "5ku2mug7m8" as the string. The schema was created correctly (in pic) but when accessing the schema, an error of
"ERROR: syntax error at or near "5"
LINE 1: SELECT * FROM 5ku2mug7m8.tablename"
which I gather is due to the leading numeric. I tried wrapping the schema in double-quotes ("") but postgres doesn't accept that.
My question is: if I cannot access a schema with a leading numeric, why does postgres allow the creation of it in the first place? Now I have sitting in the DB a schema that can't be accessed -- unless it can be and I'm just missing it?
EDIT: From documentation,
SQL identifiers and key words must begin with a letter (a-z, but also
letters with diacritical marks and non-Latin letters) or an underscore
(_). Subsequent characters in an identifier or key word can be
letters, underscores, digits (0-9), or dollar signs ($). Note that
dollar signs are not allowed in identifiers according to the letter of
the SQL standard, so their use might render applications less
portable...
But why allow creation of the schema without any errors in the first place? Curious.

You probably created the schema with pgAdmin, and that tool automatically added the double quotes for you.
You can certainly delete the schema with
DROP SCHEMA "5ku2mug7m8";
unless you have nasty things like leading or trailing blanks in the schema name.
You can find out the real schema name with:
SELECT quote_ident(nspname)
FROM pg_namespace
WHERE nspname LIKE '%5%';
Your experience and your research of the documentation is valuable. You have seen why it is a bad idea to pick object or column names (“identifiers”) that are valid SQL identifiers and don't need double quotes.

You can create a schema whose name starts with a digit if the schema name is using double quotes.
Demo with psql:
# select version();
version
--------------------------------------------------------------------------------
-------------------------
PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (R
ed Hat 4.8.5-39), 64-bit
# create schema "5ku";
CREATE SCHEMA
# create table "5ku".t(x int);
CREATE TABLE
# select * from "5ku".t;
x
---
(0 rows)

Related

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

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))";

when table name include a DOT , JPA throw "INSERT command denied to user for table 'XXX'"

I have a strange issue with JPA. I have renamed a table in my database from "Users" to "App.Users". And put this annotation to my entity class: #Table(name="App.Users") then I got this error:
INSERT command denied to user 'XXXXX'#'XXXXX' for table 'Users'
I am using MariaDB.
Please read the chapter Identifier names of MariaDB documentation.
Identifiers with characters which are not ANSI characters in the range [0-9,a-z,A-Z$_] and which are not unicode characters >= 0x0080FF have to be quoted in backticks.
The decimal point in unquoted identifiers additionally has a special meaning: In your example it means schema/database "App" and table "Users". Since the user has no privileges on database App (event if it doesn't exist) a privilege error occurred.

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;

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?

Firebird and Table,field name lowercase

I have converted a database from MySQL to Firebird, all tables name and field name are in lowercase, when I query the database, it gives me an error of table not found, because Firebird automatically converted the name of table in the query in UPPERCASE letters, but my table name in database is lowercase.
To query the database, I'm required to enclose the name of the table or the name of the field in double quotes, for example:
SELECT "field1","field2" FROM "table"
Is there a setting in Firebird to allow to query database with table/field name in lowercase letter without quoting it?
No. BTW, I suggest you to always create DB objects in uppercase (and without double quotes) in Firebird. This simplifies a lot, since you can access them writing in any case (even mixed case).