I have the following query for creating a table,
CREATE TABLE IF NOT EXISTS company (
id uuid CONSTRAINT companyid PRIMARY KEY DEFAULT gen_random_uuid(),
name varchar(128) NOT NULL,
db_uri varchar(255) NOT NULL,
c_uri varchar(255) NOT NULL,
date_c timestamp DEFAULT now(),
date_m timestamp DEFAULT now()
) WITH (fillfactor=90);
I am getting the following error when I run it through pgAdminIII.
ERROR: function gen_random_uuid() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function gen_random_uuid() does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
I have created an extension pgcrypto as that contains gen_random_uuid() function definition but that also did not help.
I ran the same scripts on a MAC and it all worked fine.
I am using PostgreSQL 9.3.
The issue was with Version of PostgreSQL.
It is in PostgreSQL 9.4 up that, pgcrypto has the gen_random_uuid() function.
Got the new version installed and it was all good!
http://www.postgresql.org/docs/9.3/static/pgcrypto.html
http://www.postgresql.org/docs/9.4/static/pgcrypto.html
Related
I was tryiong to switch from MSSQL to PostgreSQL and hence trying to convert queries to PostgreSQL equivalent. However running PostgreSQL query is giving an error:
ERROR: type "uniqueidentifier" does not exist LINE 3: ID
UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NO...
^ SQL state: 42704 Character: 38
MSSQL
CREATE TABLE [dbo].[ISS_AUDIT]
(
[ID] UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL,
[GRAPH_ID] [varchar](196)
PRIMARY KEY(ID)
);
PostgreSQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT
(
ID UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);
Am I missing something on UNIQUEIDENTIFIER ?
This is the correct script:
CREATE TABLE public.ISS_AUDIT
(
ID uuid PRIMARY KEY DEFAULT UUID_GENERATE_V4(),
GRAPH_ID VARCHAR(196)
);
See this link. Extract:
SQL Server calls the type UniqueIdentifier and PostgreSQL calls the
type uuid. Both types occupy 16-bytes of storage. For compatibility
reasons with other software or databases, many use some stanardized
text representation of them particularly for transport rather than
using the native type.
We need UUID which can be used as below:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT(
ID UUID DEFAULT UUID_GENERATE_V4()::UUID NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);
I'm getting error while I use Exclude constraint using gist
ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);
ERROR: data type uuid has no default operator class for access method "gist"
HINT: You must specify an operator class for the index or define a default operator class for the data type.
Note:
base_id datatype is uuid,
lifetime datatype is period
I am using PostgreSQL 9.4. I have to use 9.4 only as I don't have any other option since I am unable to install temporal extension in 9.5, 9.6 and 10 are gives an error.
You'll need the btree_gist extension for that:
btree_gist provides GiST index operator classes that implement B-tree equivalent behavior for the data types int2, int4, int8, float4, float8, numeric, timestamp with time zone, timestamp without time zone, time with time zone, time without time zone, date, interval, oid, money, char, varchar, text, bytea, bit, varbit, macaddr, macaddr8, inet, cidr, uuid, and all enum types.
Unfortunately support for uuid was only added in v10.
With v10, you should be able to use
base_id gist_uuid_ops WITH =
in your exclusion constraint.
With 9.4, you could cast the column to a different type first:
(base_id::text) gist_text_ops WITH =
The accepted answer is correct, btree_gist is needed, however the suggested solution does not work (at least not in v12 in 2021). If you've been getting errors like in original question you should do the following:
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);
As a bonus, I've been using it in Elixir & Ecto 3.5 and here's how to do this in Ecto migration:
execute "CREATE EXTENSION IF NOT EXISTS btree_gist"
create constraint(:tbl_product, "constraint_name", exclude: ~s|gist ("base_id" WITH =, lifetime WITH &&)|)
I have set up Sql Replication using Postgres/Npgsql.
We are using Guids for ids in Ravendb.
Everything is working fine as long as my id column in Postgres is of type varchar, but if I set it to uuid, which should be the correct type to match Guid, it fails.
It also fails for other columns than id.
Postgres log gives me:
operator does not exist: uuid = text at character 34
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Postgres schema looks like this:
CREATE TABLE public.materiels
(
id uuid NOT NULL,
type character varying(50),
nummer integer,
...
CONSTRAINT materiels_pkey PRIMARY KEY (id)
)
Replacing first line with
id character varying(50) NOT NULL
will make it work.
My replication setup looks like this:
If I set the replication up to use MSSql it works using MSSql's uniqueidentifier data type.
If you want to compare UUID with TEXT, then you need to create operators for that. The one solving your error would look like this:
CREATE FUNCTION uuid_equal_text(uuid, text)
RETURNS boolean
LANGUAGE SQL IMMUTABLE
AS
$body$
SELECT $1 = $2::uuid
$body$;
CREATE OPERATOR =(
PROCEDURE = uuid_equal_text,
LEFTARG = uuid,
RIGHTARG = text);
EDIT: Alternate solution suggested by author of this question himself:
CREATE CAST (text AS uuid)
WITH INOUT
AS IMPLICIT;
I'm using Postgres 9.1. and the auto_increment (serial) is not working. I've just found this about 'serial':
https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL
CREATE TYPE FAMILY AS(
id int,
name VARCHAR(35),
img_address VARCHAR(150));
CREATE TABLE FAMILIES of FAMILY(
id SERIAL primary key NOT NULL,
name NOT NULL
);
ERROR: syntax error at or near "SERIAL"
LINE 7: id SERIAL primary key NOT NULL,
^
********** Error **********
ERROR: syntax error at or near "SERIAL"
SQL state: 42601
When you create a table using the syntax:
CREATE TABLE xxx OF yyyy
you can add default values and constraints, but not alter or specify the type of the columns.
The type SERIAL is in effect a combination of a data type, NOT NULL constraint and default value specification. It is equivalent to:
integer NOT NULL DEFAULT nextval('tablename_colname_seq')
See: documentation for SERIAL
So, instead you would have to use:
CREATE SEQUENCE families_id_seq;
CREATE TABLE FAMILIES of FAMILY(
id WITH OPTIONS NOT NULL DEFAULT nextval('families_id_seq'),
name WITH OPTIONS NOT NULL
);
ALTER SEQUENCE families_id_seq OWNED BY FAMILIES.id;
You would have to create the sequence families_id_seq manually as well, as shown above.
I've got a PgSQL 9.4.3 server setup and previously I was only using the public schema and for example I created a table like this:
CREATE TABLE ma_accessed_by_members_tracking (
reference bigserial NOT NULL,
ma_reference bigint NOT NULL,
membership_reference bigint NOT NULL,
date_accessed timestamp without time zone,
points_awarded bigint NOT NULL
);
Using the Windows Program PgAdmin III I can see it created the proper information and sequence.
However I've recently added another schema called "test" to the same database and created the exact same table, just like before.
However this time I see:
CREATE TABLE test.ma_accessed_by_members_tracking
(
reference bigint NOT NULL DEFAULT nextval('ma_accessed_by_members_tracking_reference_seq'::regclass),
ma_reference bigint NOT NULL,
membership_reference bigint NOT NULL,
date_accessed timestamp without time zone,
points_awarded bigint NOT NULL
);
My question / curiosity is why in a public schema the reference shows bigserial but in the test schema reference shows bigint with a nextval?
Both work as expected. I just do not understand why the difference in schema's would show different table creations. I realize that bigint and bigserial allow the same volume of ints to be used.
Merely A Notational Convenience
According to the documentation on Serial Types, smallserial, serial, and bigserial are not true data types. Rather, they are a notation to create at once both sequence and column with default value pointing to that sequence.
I created test table on schema public. The command psql \d shows bigint column type. Maybe it's PgAdmin behavior ?
Update
I checked PgAdmin source code. In function pgColumn::GetDefinition() it scans table pg_depend for auto dependency and when found it - replaces bigint with bigserial to simulate original table create code.
When you create a serial column in the standard way:
CREATE TABLE new_table (
new_id serial);
Postgres creates a sequence with commands:
CREATE SEQUENCE new_table_new_id_seq ...
ALTER SEQUENCE new_table_new_id_seq OWNED BY new_table.new_id;
From documentation: The OWNED BY option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well.
Standard name of a sequence is built from table name, column name and suffix _seq.
If a serial column was created in such a way, PgAdmin shows its type as serial.
If a sequence has non-standard name or is not associated with a column, PgAdmin shows nextval() as default value.