What column type should UUID be in postgreSQL? - postgresql

I would like to use UUIDs has my primary key, and I am generating them using the built-in gen_random_uuid() expression for DEFAULT.
However, I don't know what column type to use for UUIDs. When I use uuid or UUID, I get the following error:
PostgreSQL said: column "id" cannot be cast automatically to type uuid
Hint: You might need to specify "USING id::uuid".
Is there a native UUID column type? Should I just be using varchar(255)?
Thanks.

PostgreSQL supports a UUID data type 'out of the box' indeed: https://www.postgresql.org/docs/current/datatype-uuid.html

Related

What type of code is used to create sequence in POSTGRESQL13?

Recently Which datatype is used for generating sequence in postgres13 ?
Creating Sequence separately or Using serial or Identity datatype ?
Which is best and why ?
Also Mapping between datatype is easy ?
As documented in the manual default data type is bigint if you don't specify a type.
The optional clause AS data_type specifies the data type of the sequence. [...] bigint is the default. The data type determines the default minimum and maximum values of the sequence.
(emphasis mine)
However with a current Postgres version it is recommended to use identity columns instead of serial
As documented in the manual you can control the details (data type most importantly) of the underlying sequence when you declare an identity column.

Postgres Altering column type using user-defined function

I have a column which is stored as TEXT, I would like to change it to UUID in postgres. But I need to run some function on the column first to change it to UUID. Is it possible to do smth like that?
ALTER TABLE am.product ALTER COLUMN p_merchant_id TYPE UUID USING myFunc;
Where myFunc returns UUID with the input merchant_id?
Yes, that's possible.
You just need to pass the old value to your function:
ALTER TABLE am.product
ALTER COLUMN p_merchant_id TYPE UUID USING myfunc(p_merchant_id);

Graphql mutation for a column of type UUID[]

I'm using a postgraphql autogenerated mutation to create a row with graphql in a in a postgres table that has a column of datatype UUID[].
However, there doesn't seem to be a way to save this UUID[] data with graphql? Is this a datatype that graphql doesn't account for or am I wrongly forming the array?
When I go to create the row in graphiql:
mutation {
createJob(
input: {
user_ids: ["5b7323ac-e235-4edb-bbf9-97495d9a42a1"],
instructions: "Job Instructions",
publishedDate: "2017-06-07"}
}
)
}
I get the following error:
"message": "column \"user_ids\" is of type uuid[] but expression is of type text[]"
Is a UUID technically not stored like text? I've tried different ways of forming the the UUID array but nothing seems to work
You can probably have a workaround to this problem by creating an implicit cast:
CREATE CAST (text[] AS uuid[])
WITH INOUT
AS IMPLICIT ;
PostgreSQL will automatically do the conversion from text[] to uuid[]; which just seems that is the needed step.
NOTE: The above code must be executed from "the owner of type uuid[] or text[]", which usually means: postgres or the database owner.
To check that it works, you may just do:
CREATE TABLE t
(
ids uuid[]
) ;
INSERT INTO t
(ids)
VALUES
(ARRAY['5b7323ac-e235-4edb-bbf9-97495d9a42a1','5b7323ac-e235-4edb-bbf9-97495d9a42a2']),
(ARRAY['6b7323ac-e235-4edb-bbf9-97495d9a42a1']) ;
... and then, test it with GraphQL
This was a bug in PostGraphQL; it's been fixed in version 4 (which has not been released yet, but you can install via npm install postgraphql#next)
More information: https://github.com/postgraphql/postgraphql/issues/516

PostgreSQL 9.3: Migrate text column to macaddr type

I have a table with a text column representing a MAC address, but I want to use the macaddr type instead. So, I tried:
alter table mytable alter column mac_column type macaddr;
And got this error
ERROR: column "mac_column" cannot be cast automatically to type macaddr
HINT: Specify a USING expression to perform the conversion.
But I don't know what to use as USING expression:
alter table mytable alter column mac_column type macaddr using(????????)
What should I use as USING expression?
Many thanks in advance
You can’t simply change the data type because data is already there in the column. Since the data is of type String PostgreSQL can't expect it as macaddr though you entered valid String representation of the macaddr. So now, as PostgreSQL suggested you can use the ‘USING’ expression to cast your data into macaddr.
ALTER TABLE mytable ALTER COLUMN mac_column TYPE macaddr USING(mac_column::macaddr)

get index from postgresql sequence using liquibase

What attribute of column I should use in order to get index value from postgresql sequence? valueNumeric? valueComputed?
As far as I understand the value of attribute should be nextval( 'simple_id_seq' ).
In postgresql sequence values are created as INTEGER or BIGINT.
Often this was done by using SERIAL or BIGSERIAL as column type ... but will indirectly create a sequencer of int or bigint and set the default value of the column to nextval(sequencer).
In a resultset of table data the column contains int or bigint.
Normaly there is no need to use nextval(sequencer) ... it fills the column on INSERT automatically (in the INSERT statemant the column shoult not appear).
Refer to http://www.postgresql.org/docs/9.3/static/datatype-numeric.html
If you do not want to use SERIAL or BIGSERIAL as suggested by #double_word_distruptor, use valueComputed.
With valueComputed you are telling Liquibase you are passing a function like nextval('simple_id_seq') and it will not try to parse it as a number or do any quoting.
You may also be able to use valueSequenceNext="simple_id_seq" to gain a little cross-database compatibility.