column does not exist is postgresql - postgresql

Here is my sql data.
When I execute this query in postgresql getting some error.
Please help me to correct this. I have no experience in this database.
-- ----------------------------
-- Sequence structure for tuning_id_seq
-- ----------------------------
DROP SEQUENCE IF EXISTS "mainpage"."tuning_id_seq";
CREATE SEQUENCE "mainpage"."tuning_id_seq" INCREMENT 1 START 4000 MAXVALUE 9223372036854775807 MINVALUE 1 CACHE 1;
ALTER TABLE "mainpage"."tuning_id_seq" OWNER TO "postgres";
-- ----------------------------
-- Table structure for tuning
-- ----------------------------
DROP TABLE IF EXISTS "mainpage"."tuning";
CREATE TABLE "mainpage"."tuning" (
"id" int4 NOT NULL DEFAULT nextval("tuning_id_seq"::regclass),
"motor_id" int4,
"speed" int4,
"freetext" varchar(200) COLLATE "default",
"date_create" varchar(30) COLLATE "default",
"date_change" varchar(30) COLLATE "default"
)
WITH (OIDS=FALSE);
ALTER TABLE "mainpage"."tuning" OWNER TO "postgres";
-- ----------------------------
-- Records of tuning
-- ----------------------------
BEGIN;
INSERT INTO "mainpage"."tuning" VALUES ('1', '1', '0', null, null, null);
INSERT INTO "mainpage"."tuning" VALUES ('2', '2', '0', null, null, null);
INSERT INTO "mainpage"."tuning" VALUES ('3', '3', '0', null, null, null);
COMMIT;
Error
NOTICE: table "tuning" does not exist, skipping
ERROR: column "tuning_id_seq" does not exist
********** Error **********
ERROR: column "tuning_id_seq" does not exist
SQL state: 42703

I think the issue is you need to schema-qualify your sequence name.
e.g.:
CREATE TABLE "mainpage"."tuning" (
"id" int4 NOT NULL DEFAULT nextval("mainpage"."tuning_id_seq"::regclass),
"motor_id" int4,
"speed" int4,
"freetext" varchar(200) COLLATE "default",
"date_create" varchar(30) COLLATE "default",
"date_change" varchar(30) COLLATE "default"
)
WITH (OIDS=FALSE);
ALTER TABLE "mainpage"."tuning" OWNER TO "postgres";
Without the qualification, Postgres will look for the sequence in the public (e.g. unqualified) schema. (Since you could, in theory, have a sequence by the same name in each schema).

Related

Migrating DB2 Blob/Rowid data types to PostgreSQL

How would I create the following DB2 table in PostgreSQL?
CREATE TABLE table1 (
idDoc DECIMAL(15) NOT NULL,
rowId ROWID NOT NULL,
checksumDoc DECIMAL(15) NOT NULL,
doc BLOB NOT NULL,
PRIMARY KEY (idDoc)
);
I came up with the following DDL for PostgreSQL, is this correct?
CREATE TABLE table1 (
idDoc DECIMAL(15) NOT NULL,
checksumDoc DECIMAL(15) DEFAULT 0 NOT NULL,
doc bytea NOT NULL DEFAULT E'\\000'
);
Can I omit the ROWID?

postgresql cannot insert data to newly added column

In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.

Getting error on Postgresql TABLESPACE and CREATE commands

I am completely new in Postgres commands.
I have the following DB2 commands to create tables and table spaces:
CREATE USER TEMPORARY TABLESPACE MYSPACE MANAGED BY AUTOMATIC STORAGE#
CREATE TABLESPACE SYSTOOLSPACE MANAGED BY AUTOMATIC STORAGE#
CREATE SEQUENCE REVISION AS BIGINT START WITH 1 INCREMENT BY 1 MAXVALUE 4611686018427387903 CYCLE CACHE 1000#
Now i want to run these commands in postgresql , I have tried with
my_db=# CREATE USER TEMPORARY TABLESPACE MYSPACE MANAGED BY AUTOMATIC STORAGE;
ERROR: syntax error at or near "TABLESPACE"
LINE 1: CREATE USER TEMPORARY TABLESPACE MYSPACE MANAGED BY AUTO...
my_db=# CREATE TABLESPACE SYSTOOLSPACE MANAGED BY AUTOMATIC STORAGE;
ERROR: syntax error at or near "MANAGED"
LINE 1: CREATE TABLESPACE SYSTOOLSPACE MANAGED BY AUTOMATIC STORAGE;
I also have this DB2 CREATE TABLE statement:
CREATE TABLE USER (
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE CACHE 100),
E_VER BIGINT NOT NULL,
NAME VARCHAR(38) NOT NULL UNIQUE,
EMAIL_ADDRESS VARCHAR(255) NOT NULL,
PASSWORD VARCHAR(32) NOT NULL,
SUPER_ADMIN SMALLINT NOT NULL,
MAIN_ADMIN SMALLINT NOT NULL,
SERVER_ADMIN SMALLINT NOT NULL,
GROUP_ADMIN SMALLINT NOT NULL,
CLIENT_ADMIN SMALLINT NOT NULL,
ENABLED SMALLINT NOT NULL,
HIDDEN SMALLINT NOT NULL,
PRIMARY KEY (ID)
)#
and I have tried to convert this to Postgres:
CREATE SEQUENCE USER_seq START WITH 1 INCREMENT BY 1;
CREATE TABLE USER (
ID BIGINT DEFAULT NEXTVAL ('USER_seq'),
E_VER BIGINT NOT NULL,
NAME VARCHAR(38) NOT NULL UNIQUE,
EMAIL_ADDRESS VARCHAR(255) NOT NULL,
PASSWORD VARCHAR(32) NOT NULL,
SUPER_ADMIN SMALLINT NOT NULL,
MAIN_ADMIN SMALLINT NOT NULL,
SERVER_ADMIN SMALLINT NOT NULL,
GROUP_ADMIN SMALLINT NOT NULL,
CLIENT_ADMIN SMALLINT NOT NULL,
ENABLED SMALLINT NOT NULL,
HIDDEN SMALLINT NOT NULL,
PRIMARY KEY (ID)
)#
by http://www.sqlines.com/online this online site. After running this command i am getting error like
my_db=# CREATE SEQUENCE USER_seq START WITH 1 INCREMENT BY 1;
CREATE SEQUENCE
my_db=#
my_db=#
my_db=# CREATE TABLE USER (
my_db(# ID BIGINT DEFAULT NEXTVAL ('USER_seq'),
my_db(# E_VER BIGINT NOT NULL,
my_db(# NAME VARCHAR(38) NOT NULL UNIQUE,
my_db(# EMAIL_ADDRESS VARCHAR(255) NOT NULL,
my_db(# PASSWORD VARCHAR(32) NOT NULL,
my_db(# SUPER_ADMIN SMALLINT NOT NULL,
my_db(# MAIN_ADMIN SMALLINT NOT NULL,
my_db(# SERVER_ADMIN SMALLINT NOT NULL,
my_db(# GROUP_ADMIN SMALLINT NOT NULL,
my_db(# CLIENT_ADMIN SMALLINT NOT NULL,
my_db(# ENABLED SMALLINT NOT NULL,
my_db(# HIDDEN SMALLINT NOT NULL,
my_db(# PRIMARY KEY (ID)
my_db(# );
ERROR: syntax error at or near "USER"
LINE 1: CREATE TABLE USER (
^
Anything wrong this conversion? Any suggestion solve this error?
USER is a reserved word, you need to escape it as CREATE TABLE "USER".
As for your CREATE USER and CREATE TABLESPACE commands, that's just wrong syntax. There's no MANAGED BY in Postgres for example.

forward engineer with mysql workbench cannot create the schema

-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `motivian` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `motivian` ;
-- -----------------------------------------------------
-- Table `motivian`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`user` ;
CREATE TABLE IF NOT EXISTS `motivian`.`user` (
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NULL,
`password` VARCHAR(100) NOT NULL,
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `motivian`.`calculation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`calculation` ;
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
This is auto generated with mysql workbench but the constraint could not be applied. I tried like this:
CONSTRAINT user_id
FOREIGN KEY (user_id)
REFERENCESmotivian.user(user_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
but also don't work.....
This is the error:
Executing SQL script in server
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO A' at line 8
SQL Code:
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Your FK definition is incomplete. Youd did not specify any column. Select your foreign key in the table editor and specify column pairs in the list right to it:

pgAdmin - When trying to make a foreign key "referencing" gives no options

I have three tables: ModelingAgency.clients, ModelingAgency.models, ModelingAgency.Bookings. All three tables have a primary key column called id.
The table bookings has two columns that reference clients and models. In pgAdmin when I try to create a foreign key in bookings to either clients or models I get the following screens:
What am I overlooking here? I am new to PostgreSQL (This is my first test project with PostgreSQL -- I've always used MySQL and occasionally SQL Server) so it's probably something obvious (I just don't see it).
EDIT: Here is the DDL, as requested:
-- Table: "ModelingAgency.bookings"
-- DROP TABLE "ModelingAgency.bookings";
CREATE TABLE "ModelingAgency.bookings"
(
id integer NOT NULL DEFAULT nextval('"ModelingAgency.Bookings_id_seq"'::regclass),
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.bookings" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.clients"
-- DROP TABLE "ModelingAgency.clients";
CREATE TABLE "ModelingAgency.clients"
(
id integer NOT NULL DEFAULT nextval('"ModelAgency.clients_id_seq"'::regclass),
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.clients" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.models"
-- DROP TABLE "ModelingAgency.models";
CREATE TABLE "ModelingAgency.models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.models" OWNER TO "MyBatisTutorial";
Looking into your posted DDL code I see that your table's names are written in wrong way (that causes your issue with pgAdmin):
"ModelingAgency.bookings"
It should be in format "schema"."tableName":
"ModelingAgency"."bookings"
After that Object browser looks like this (probably you need to create schema first using easily pgAdmin or with CREATE SCHEMA SQL statement):
Here is working DDL code (I omitted some things like OIDS and OWNER TO, but that doesn't matter to your case, BTW OIDS are false on default):
DROP TABLE IF EXISTS "ModelingAgency"."bookings";
CREATE TABLE "ModelingAgency"."bookings"
(
id serial,
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL
DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."clients";
CREATE TABLE "ModelingAgency"."clients"
(
id serial,
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."models";
CREATE TABLE "ModelingAgency"."models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)