Joomla extension installation with PostgreSQL support - postgresql

I would like to support PostgreSQL with my extension, however I'm running into a problem when I attempt to install it. Currently, I have the following in my XML for the install section:
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
<file driver="postgresql" charset="utf8">sql/install.postgresql.utf8.sql</file>
</sql>
</install>
And here is the code for install.postgresql.utf8.sql:
CREATE TABLE "#__shoutbox" (
"id" serial NOT NULL,
"name" character varying(25) DEFAULT '' NOT NULL,
"when" timestamp without time zone DEFAULT '' NOT NULL,
"ip" character varying(15) DEFAULT '' NOT NULL,
"msg" text NOT NULL,
"user_id" bigint(11) DEFAULT 0 NOT NULL,
PRIMARY KEY ("id")
);
INSERT INTO "#__shoutbox" ("name", "when", "msg", "user_id") VALUES ('JoomJunk', '2013-04-04 20:00:00', 'Welcome to the Shoutbox', '0');
I have noticed a few differences between the query for MySQL and PostgreSQL which I think I have taken into consideration, but when I attempt to install the extension, I get the following error:
Database query failed (error # %s): %s SQL=CREATE TABLE "pdo31_shoutbox" ( "id" serial NOT NULL, "name" character varying(25) DEFAULT '' NOT NULL, "when" timestamp without time zone DEFAULT '' NOT NULL, "ip" character varying(15) DEFAULT '' NOT NULL, "msg" text NOT NULL, "user_id" bigint(11) DEFAULT 0 NOT NULL, PRIMARY KEY ("id") );
There is no documentation on supporting PostgreSQL for extensions so I have done what I can by looking at the SQL file from the Joomla 3.1 installation folder.
Is the problem with my query?

The default value for when is not a valid timestamp. If you want when to be empty then remove the NOT NULL constraint so it can be NULL. Otherwise specify a valid timestamp like '2013-4-4 12:34:56'.
Bigint in postgresql does not support a size specification. It is always 64-bits (which is more then 11 decimal digits).
CREATE TABLE "pdo31_shoutbox" (
"id" serial NOT NULL,
"name" character varying(25) DEFAULT '' NOT NULL,
"when" timestamp without time zone,
"ip" character varying(15) DEFAULT '' NOT NULL,
"msg" text NOT NULL,
"user_id" bigint DEFAULT 0 NOT NULL,
PRIMARY KEY ("id")
);

Related

Getting error while importing PostgreSQL database?

CREATE TABLE IF NOT EXISTS "access_log" (
"id" BIGINT NOT NULL DEFAULT 'nextval(''access_log_id_seq''::regclass)',
"user_id" BIGINT NOT NULL,
"platform" SMALLINT NOT NULL,
"created_at" TIMESTAMP NULL DEFAULT NULL,
"updated_at" TIMESTAMP NULL DEFAULT NULL,
"device_id" VARCHAR(255) NULL DEFAULT NULL,
"location" VARCHAR(255) NULL DEFAULT NULL,
"ip" VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "access_log_user_id_foreign" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
I am trying to restore my PostgreSQL using HeidiSQL. It is showing invalid input syntax for type bigint.
The call of the nextval() function must not be quoted.
So this:
default 'nextval(''access_log_id_seq''::regclass)'
needs to be:
default nextval('access_log_id_seq'::regclass)

ERROR: column "username" of relation "public.ae_User" does not exist - Postgresql [duplicate]

This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
Closed 1 year ago.
I'm learning postgresql and make this table in elephantsql:
CREATE TABLE "public.ae_User" (
"Id" serial NOT NULL,
"Name" character varying(30) NOT NULL,
"Username" character varying(16) NOT NULL UNIQUE DEFAULT 'Guest',
"Email" character varying(30) NOT NULL,
"Password" character varying(120) NOT NULL,
"Salt" character varying(40) NOT NULL,
"User_type" int NOT NULL DEFAULT '0',
"Deleted" BOOLEAN NOT NULL DEFAULT 'false',
CONSTRAINT "ae_User_pk" PRIMARY KEY ("Id")
) WITH (
OIDS=FALSE
);
But when I try to do an insert into:
INSERT INTO "public"."public.ae_User" (Name, Username, Email, Password, Salt, User_type, Deleted)
VALUES ("Miguel", "adminMiguel", "miguel#gmail.com", "admin", "bla", 3, false)
It returns:
ERROR: column "username" of relation "public.ae_User" does not exist
LINE 1: INSERT INTO "public"."public.ae_User" (Username, Email, Pass...
You should use a double-quoted in the column name because Postgres is folded column to lowercase.
If the column contains uppercase (and/or other syntax violations) you have to use double-quoted
On other hand for text or varchar data has been passed to the column you have to use single-quoted
INSERT INTO "public"."public.ae_User" ("Name", "Username", "Email", "Password", "Salt", "User_type", "Deleted")
VALUES ('Miguel', 'adminMiguel', 'miguel#gmail.com', 'admin', 'bla', 3, false)
P.S:
In my opinion, better change your structure like below:
CREATE TABLE public."ae_User" (
"Id" serial NOT NULL,
"Name" character varying(30) NOT NULL,
"Username" character varying(16) NOT NULL UNIQUE DEFAULT 'Guest',
"Email" character varying(30) NOT NULL,
"Password" character varying(120) NOT NULL,
"Salt" character varying(40) NOT NULL,
"User_type" int NOT NULL DEFAULT '0',
"Deleted" BOOLEAN NOT NULL DEFAULT 'false',
CONSTRAINT "ae_User_pk" PRIMARY KEY ("Id")
) WITH (
OIDS=FALSE
);
INSERT INTO public."ae_User" ("Name", "Username", "Email", "Password", "Salt", "User_type", "Deleted")
VALUES ('Miguel', 'adminMiguel', 'miguel#gmail.com', 'admin', 'bla', 3, false)
your error is in INSERT INTO "public"."public.ae_User" to be replaced by INSERT INTO "public"."ae_User"

Why regenerated Postgres table raise error in Vapor?

A table in production environment was created like this:
-- DDL generated by Postico 1.5.10
-- Not all database features are supported. Do not use for backup.
-- Table Definition ----------------------------------------------
CREATE TABLE "TempUser" (
id bigint PRIMARY KEY,
"Uuid" text NOT NULL,
"dateCreated" timestamp without time zone NOT NULL,
"lastRequestDate" timestamp without time zone NOT NULL,
"lastRequestLocation" text,
"numOfRequest" bigint NOT NULL,
comment text NOT NULL,
"lastRequestDescription" text NOT NULL,
"bookedSeats" text NOT NULL,
"bookUntil" timestamp without time zone,
"lastEventVisited" text
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX "pk:TempUser.id" ON "TempUser"(id int8_ops);
Why I remove table in dev environment and create like this in Postgres console, I got following error after try writing programmatically:
[ ERROR ] PostgreSQLError.server.error.ExecConstraints: POST /registerTempUser null value in column "id" violates not-null constraint (ErrorMiddleware.swift:26)
[ DEBUG ] Possible causes for PostgreSQLError.server.error.ExecConstraints: Failing row contains (null, 015574220836500, 2020-07-09 19:42:36.803846, 2020-07-09 19:42:36.803846, null, 1, , POST /registerTempUser HTTP/1.1
Host: localhost:8080
Content-Typ..., [], null, null). (ErrorMiddleware.swift:26)
Why, what is wrong?
Tried set sequence thing:
select setval(pg_get_serial_sequence('"TempUser"', 'id'), 1);
select currval(pg_get_serial_sequence('"TempUser"', 'id'));
nothing changed.

How to get primary key sequence in Postgres?

I have following table:
-- DDL generated by Postico 1.5.10
-- Not all database features are supported. Do not use for backup.
-- Table Definition ----------------------------------------------
CREATE TABLE "Ticket" (
id bigint PRIMARY KEY,
"paymentId" text NOT NULL,
"transactionId" text,
"dateCreated" timestamp without time zone NOT NULL,
"dateValidated" timestamp without time zone,
"sellerPaymentId" text NOT NULL,
"sellerPaymentProvider" text NOT NULL,
"userId" bigint NOT NULL,
"userIdFb" text NOT NULL,
"userName" text NOT NULL,
"eventDescription" text NOT NULL,
"eventTimeId" text,
"eventId" text NOT NULL,
"eventName" text NOT NULL,
"startTime" timestamp without time zone,
"endTime" timestamp without time zone,
quantity bigint,
"unitPrice" text,
seats jsonb[],
location text NOT NULL,
link text,
"eventTimesSelected" jsonb,
"otherListsSelected" jsonb,
"transactionIdBarion1" text,
"transactionIdBarion2" text
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX "pk:Ticket.id" ON "Ticket"(id int8_ops);
When inserting a new row, got this error:
[ ERROR ] PostgreSQLError.server.error._bt_check_unique: POST /startPayment duplicate key value violates unique constraint "pk:Ticket.id" (ErrorMiddleware.swift:26)
[ DEBUG ] Possible causes for PostgreSQLError.server.error._bt_check_unique: Key (id)=(1) already exists. (ErrorMiddleware.swift:26)
How the heck I can reset the primary key sequence? There are many answers on internet, but what is the name of my sequence? :) I do not see any 'name' in my DDL.
I tried fetch sequence name like this:
select currval(pg_get_serial_sequence("Ticket", "id"));
no luck:
ERROR: column "Ticket" does not exist
LINE 1: select currval(pg_get_serial_sequence("Ticket", "id"));
pg_get_serial_sequence() expects string values, not identifiers. Your problems stem from the fact that you used those dreaded quoted identifiers when creating the table which is strongly discouraged.
You need to pass the double quotes inside single quotes:
select currval(pg_get_serial_sequence('"Ticket"', '"id"'));
You should reconsider the usage of quoted identifiers to avoid problems like that in the future.
How the heck I can reset the primary key sequence
How to reset postgres' primary key sequence when it falls out of sync?
How to bulk update sequence ID postgreSQL for all tables

Postgres Update table id with sequence

I am trying to connect a sequence for user table to auto incremental value for id field.
I created following sequence,
CREATE SEQUENCE "USER_MGMT"."USER_SEQ"
INCREMENT 1
START 1000
MINVALUE 1000
MAXVALUE 99999999
CACHE 1;
ALTER SEQUENCE "USER_MGMT"."USER_SEQ"
OWNER TO postgres;
following is my table,
-- Table: "USER_MGMT"."USER"
-- DROP TABLE "USER_MGMT"."USER";
CREATE TABLE "USER_MGMT"."USER"
(
"USER_ID" bigint NOT NULL,
"FIRST_NAME" character varying(50) COLLATE pg_catalog."default" NOT NULL,
"LAST_NAME" character varying(50) COLLATE pg_catalog."default" NOT NULL,
"EMAIL_ID" character varying(100) COLLATE pg_catalog."default" NOT NULL,
"DESK_NUMBER" bigint,
"MOBILE_NUMBER" bigint,
"IS_ACTIVE" boolean NOT NULL DEFAULT true,
"CREATED_BY" character varying(100) COLLATE pg_catalog."default",
"MODIFIED_BY" character varying(100) COLLATE pg_catalog."default",
"DATE_CREATED" timestamp without time zone NOT NULL,
"DATE_MODIFIED" timestamp without time zone,
CONSTRAINT "USER_ID_PK" PRIMARY KEY ("USER_ID"),
CONSTRAINT "EMAIL_ID_UK" UNIQUE ("EMAIL_ID"),
CONSTRAINT "MOBILE_NUMBER_UK" UNIQUE ("MOBILE_NUMBER"),
CONSTRAINT "USER_ID_UK" UNIQUE ("USER_ID")
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE "USER_MGMT"."USER"
OWNER to postgres;
I want to connect this sequence to USER_ID column, so it will be auto incremented.
Table name and fields should be in upper case,
I am trying to execute the following query, but its not working
ALTER TABLE USER_MGMT.USER ALTER COLUMN USER_ID SET DEFAULT nextval('USER_MGMT.USER_SEQ');
It says the following error message in console.
ERROR: schema "user_mgmt" does not exist
********** Error **********
That is because when you use double quotes then you are creating case sensitive object identifier or to be more precise - this object will have identifier with exact case as given in the query during creation. If you do not double quote them, then they are converted to lower case.
So what you need is to either stop using double quotes, create objects in lower case or use double quotes in your alter query:
ALTER TABLE "USER_MGMT"."USER" ALTER COLUMN "USER_ID" SET DEFAULT nextval('"USER_MGMT"."USER_SEQ"');