how to understand column types from sql file? - postgresql

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'

Related

Postgresql 15 - Trailing junk after numeric literal error

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.

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

Postgresql create generated column syntax error, why?

I have a postgres table with two columns (an identificator and a date) that are a composite primary key. I would like to hash the concatenation in another column, generating this value everytime a new record is inserted. For that I'm trying to alter my table in order to create a generated column:
ALTER TABLE my_table ADD COLUMN hash_id_date VARCHAR(50)
GENERATED ALWAYS AS (MD5(my_table.original_id||'-'||my_table.time))
STORED;
This raises me the following error:
ERROR: syntax error at or near "("
LINE 4: GENERATED ALWAYS AS (MD5(my_table.original_id,'-',my_table.t...
^
SQL state: 42601
Character: 178
I'm turning into madness to find where is the syntax error... I've read about STABLE and IMMUTABLE functions and generated columns should always have an IMMUTABLE function as expression. As far as I know MD5 is IMMUTABLE but the error message is not even capable to reach that level.
Any help?
Assuming the basic functionality for calculating the MD5 is common you can create a function for the calculation. Use this function wherever it's needed, including updating your current rows and invoke from a trigger on yo your table. If the particular MD5 calculation is not all that common you can just put the calculation in the trigger function and also use it in a independent update for current rows. See here for example with assumption it is common in your app.

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 determine which column is implicated in "value too long for type character varying"?

I'm programatically adding data to a PostgreSQL table using Python and psycopg - this is working fine.
Occasionally though, a text value is too long for the containing column, so I get the message:
ERROR: value too long for type character varying(1000)
where the number is the width of the offending column.
Is there a way to determine which column has caused the error? (Aside from comparing each column's length to see whether it is 1000)
Many thanks to #Tometzky, whose comment pointed me in the right direction.
Rather than trying to determine which column caused the problem after the fact, I modified my Python script to ensure that the value was truncated before inserting into the database.
access the table's schema using select column_name, data_type, character_maximum_length from information_schema.columns where table_name='test'
when building the INSERT statement, use the schema definition to identify character fields and truncate if necessary
I don't think there's an easy way.
I tried to set VERBOSITY in psql, as I assumed this would help, but unfortunately not (on 9.4):
psql
\set VERBOSITY verbose
dbname=> create temporary table test (t varchar(5));
CREATE TABLE
dbname=> insert into test values ('123456');
ERROR: 22001: value too long for type character varying(5)
LOCATION: varchar, varchar.c:623
This might be something that warrants discussion on the mailing list, as you are not the only one with this problem.