Postgresql 15 - Trailing junk after numeric literal error - postgresql

After the Postgresql update v15, I realised that even I have a column that accepts UUID data type, it will throw me similar error like this whenever I try to Insert UUID data into the table :
Script :
INSERT INTO public.testing(uuid, rating) VALUES (${uuid}, ${rating})'
Error:
error running query error: trailing junk after numeric literal at or near "45c"
Postgresql 15 release note:
Prevent numeric literals from having non-numeric trailing characters (Peter Eisentraut)
Is there any solution for this issue? Or there an alternative data type that allows storing UUID into my table?

It seems that you forgot the single quotes around the UUID, so that the PostgreSQL parser took the value for a subtraction and complained that there were letters mixed in with the digits. This may throw a different error on older PostgreSQL versions, but it won't do the right thing either.
Be careful about SQL injection when you quote the values.

Related

How do you manually insert a uuid type field in Postgres SQL DB?

I have a Postgres table with a field called user_uuid with type uuid. I also have a valid UUID value that I want to manually insert in that row, but I can't seem to find a way to manually create this.
This is an example of the statement I'm trying to execute:
insert into my_table (account_number, type, user_uuid) values ('1252', 'residential', 'dOfa6513-aOfd-4e78-9941-724b22804e9f');
I've tried appending ::UUID which I read somewhere might work, and to enclose the UUID text value inside curly brackets, instead of single quotes. None of that has worked, and the docs are not helpful either. The error I get is the following:
invalid input syntax for type uuid: 'dOfa6513-aOfd-4e78-9941-724b22804e9f'
The UUID you're trying to insert is not a valid UUID.
You can check the validity here https://www.freecodeformat.com/validate-uuid-guid.php
This is one example of a valid UUID: a8adfa00-6680-49b3-bf94-caa8c3f1d823,
can try pass this into your insert query and check if ok.
There are 2 occurrences of the letter O in your uuid.
It should have been the digit 0 instead (zero) to make it a proper hexadecimal string: d0fa6513-a0fd-4e78-9941-724b22804e9f

Npgsql.PostgresException: Column cannot be cast automatically to type bytea

Using EF-Core for PostgresSQL, I have an entity with a field of type byte but decided to change it to type byte[]. But when I do migrations, on applying the migration file generated, it threw the following exception:
Npgsql.PostgresException (0x80004005): 42804: column "Logo" cannot be
cast automatically to type bytea
I have searched the internet for a solution but all I saw were similar problems with other datatypes and not byte array. Please help.
The error says exactly what is happening... In some cases PostgreSQL allows for column type changes (e.g. int -> bigint), but in many cases where such a change is non-trivial or potentially destructive, it refuses to do so automatically. In this specific case, this happens because Npgsql maps your CLR byte field as PostgreSQL smallint (a 2-byte field), since PostgreSQL lacks a 1-byte data field. So PostgreSQL refuses to cast from smallint to bytea, which makes sense.
However, you can still do a migration by writing the data conversion yourself, from smallint to bytea. To do so, edit the generated migration, find the ALTER COLUMN ... ALTER TYPE statement and add a USING clause. As the PostgreSQL docs say, this allows you to provide the new value for the column based on the existing column (or even other columns). Specifically for converting an int (or smallint) to a bytea, use the following:
ALTER TABLE tab ALTER COLUMN col TYPE BYTEA USING set_bytea(E'0', 0, col);
If your existing column happens to contain more than a single byte (should not be an issue for you), it should get truncated. Obviously test the data coming out of this carefully.

how to understand column types from sql file?

I am not good at column types as I understand. From another country with another system they just send me a sql file and they claim that there is an image on that sql file. I guess it is byte array, however I couldnt insert it into PostgreSQL. When I try to insert it says:
LINE 1: ...ES ('00246c4e-1bc8-4dde-bb89-e9dee69990d5', '0', 0xffa0ffa40...
^
********** Error **********
ERROR: syntax error at or near "xffa0f
Could you please help me to create related table with its column properties?
I know that it is not good question, however here is starting of sql file;
INSERT INTO `fps` VALUES ('00246c4e-1bc8-4dde-bb89-e9dee69990d5', '0', 0xffa0ffa4003a0907000932d325cd000ae0f3199a010a41eff19a010b8e2......
What is the type of 0xffa0ff....?
'00246c4e-1bc8-4dde-bb89-e9dee69990d5' is a UUID.
'0' is just a character string. There are a few different string types to choose from. However, if all of these values are integers, you may want to create the column as an INTEGER instead.
0xff... is a hex string, though not in a format that Postgres will recognise. You can store this data in a bytea column, but in order for the INSERT to succeed, you will need to modify the script, replacing, for example,
0xab...ef
with
'\xab...ef'

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?

Change the character varying length with ALTER statement?

I am trying to change length of a column from character varying(40) to character varying(100).
Following the method described in this question Increasing the size of character varying type in postgres without data loss
ALTER TABLE info_table ALTER COLUMN docs TYPE character varying(100);
Tried with this command but returning syntax error
ERROR: syntax error at or near "TYPE" at character 52
Is there any change needed in this command? Using PostgreSQL version 7.4.30 (upgrade to 9.2 in process :) ).
I tried this same command in test db which is now upgraded with version 9.2. It is working fine there.
Changing the column type on the fly was not possible in the ancient version 7.4. Check the old manual. You had to add another column, update it with the (possibly transformed) values and then drop the old one, rename the new one - preferably in a single transaction. With side effects on views or other depending objects ...
To avoid this kind of problem altogether I suggest to use plain text or varchar (without length modifier) for character data. Details in this related question.
Remove the word TYPE, that syntax wasn't recognized 10 years ago, but you should be fine without it.