Postgres ALTER TABLE ...IDENTITY...SEQUENCE NAME syntax error - postgresql

I'm trying to import a dump created by pg_dump 2.9 into postgres 13.4, however it fails on the ALTER TABLE ...IDENITY...SEQUENCE NAME
CREATE TABLE admin.bidtype (
bidtype_id integer NOT NULL,
title character varying(50) NOT NULL,
created_by integer,
created_date timestamp without time zone,
updated_by integer,
updated_date timestamp without time zone,
deleted_by integer,
deleted_date timestamp without time zone
);
ALTER TABLE admin.bidtype OWNER TO app_bidhq;
--
-- Name: bidtype_bidtype_id_seq; Type: SEQUENCE; Schema: admin; Owner: postgres
--
ALTER TABLE admin.bidtype ALTER COLUMN bidtype_id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE name admin.bidtype_bidtype_id_seq
^ ^
|______________________|__________________
START 1
^
|______________________________________________________
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
CYCLE
);
The error shown is "[42601] ERROR: syntax error at end of input Position: 132" and DataGrip highlights errors at the marked positions. I'm new to Postgres, but have checked the documentation https://www.postgresql.org/docs/13/sql-altersequence.html. The syntax looks correct to me.
This is running on RDS for Postgres

Related

postgresql 10 altering serial column error 42p01

I am facing an issue when correcting an existing table to use serial column on the primary key. In order to simulate the issue I created a new table:
CREATE TABLE markets."TestSequence" (
"Id" integer NOT NULL,
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "PK_TestSequence" PRIMARY KEY ("Id")
);
Then I ran the query that is causing problem:
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" TYPE integer;
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" SET NOT NULL;
CREATE SEQUENCE "TestSequence_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" SET DEFAULT (nextval('"TestSequence_Id_seq"'));
ALTER SEQUENCE "TestSequence_Id_seq" OWNED BY "TestSequence"."Id";
I get the following error:
ERROR: relation "TestSequence" does not exist
SQL state: 42P01
According to the doc OWNED BY does not take any schema prefix. So I tried to create the table without schema and it works fine.
CREATE TABLE "TestSequence" (
"Id" integer NOT NULL,
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "PK_TestSequence" PRIMARY KEY ("Id")
);
and run the corresponding alter queries:
ALTER TABLE "TestSequence" ALTER COLUMN "Id" TYPE integer;
ALTER TABLE "TestSequence" ALTER COLUMN "Id" SET NOT NULL;
CREATE SEQUENCE "TestSequence_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
ALTER TABLE "TestSequence" ALTER COLUMN "Id" SET DEFAULT (nextval('"TestSequence_Id_seq"'));
ALTER SEQUENCE "TestSequence_Id_seq" OWNED BY "TestSequence"."Id";
How can I make this work for relations with schema?
The doc you have linked says, for owned by
The specified table must have the same owner and be in the same schema
as the sequence.
You haven't specified a schema for the sequence, so it is created in public by default, which is not the same as the table schema.
Try creating the sequence as
CREATE SEQUENCE markets."TestSequence_Id_seq" AS integer ...
That being said, nothing prevents you from specifying the schema of both the sequence and the table
ALTER SEQUENCE markets."TestSequence_Id_seq" OWNED BY markets."TestSequence"."Id";

Pg_dump -s (schema) of tables omits the defaults for columns that use nextval()

After defining the table, the script generated by PGAdmin via rightclick->SCRIPT->CREATE SCRIPT, looks like this:
-- DROP TABLE public.arr;
CREATE TABLE public.arr
(
rcdno integer NOT NULL DEFAULT nextval('arr_rcdno_seq'::regclass),
rcdstate character varying(10) COLLATE pg_catalog."default" DEFAULT 'ACTIVE'::character varying,
rcdserial integer NOT NULL DEFAULT 0,
msrno integer NOT NULL DEFAULT nextval('arr_msrno_seq'::regclass),
mobileid character varying(50) COLLATE pg_catalog."default" NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT pk_arr PRIMARY KEY (mobileid, msrno, rcdserial)
)
After using >pg_dump -s -U webmaster -W -F p Test > c:\temp\Test.sql, the table definition in the script OMITS the defaults for the 2 original "serial" columns. This means that the pg_dump script doesn't create the table correctly when it is run!
--
-- Name: arr; Type: TABLE; Schema: public; Owner: webmaster
--
CREATE TABLE public.arr (
rcdno integer NOT NULL, -- default missing!
rcdstate character varying(10) DEFAULT 'ACTIVE'::character varying,
rcdserial integer DEFAULT 0 NOT NULL,
msrno integer NOT NULL, -- default missing!
mobileid character varying(50) NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) NOT NULL
);
ALTER TABLE public.arr OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_msrno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_msrno_seq OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_msrno_seq OWNED BY public.arr.msrno;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_rcdno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_rcdno_seq OWNER TO webmaster;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_rcdno_seq OWNED BY public.arr.rcdno;
EDIT: After the generated script has been run, the statement
into public.arr(mobileid, editor)
values
('12345', 'nolaspeaker')
generates
ERROR: null value in column "rcdno" violates not-null constraint DETAIL: Failing row contains (null, ACTIVE, 0, null, 12345, 2020-08-18 08:54:41.34052, nolaspeaker). SQL state: 23502
Which indicates that I am correct to assert that the script omits the default values for the rcdno column!
The ALTER TABLE statements that set the default values are towards the end of the dump, close to where the ALTER SEQUENCE ... OWNED BY are.
If they don't get restored, that might be because there was a problem creating the sequences.
Look at the error messages from the restore, particularly the first ones (later ones are often consequences of the earlier ones).
The statements that assign the default values to the applicable columns come much later in the script
--
-- Name: arr rcdno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN rcdno SET DEFAULT nextval('public.arr_rcdno_seq'::regclass);
--
-- Name: arr msrno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN msrno SET DEFAULT nextval('public.arr_msrno_seq'::regclass);
So you have to be careful if you cut-and-paste a table definition out of the script!

SymmetricDS and Postgres with UUID primary key failed (invalid input syntax for type uuid)

I am using Postgres 11 and SymmetricDS 3.9.14.
I have a database table with primary key of UUID type. It seems like SymmetricDS is not able to cast 'UUID' correctly. One of SymmetricDS replication query is failing with the below error
JdbcSqlTemplate - SQL caused exception: [select "id" from "dbo"."groups" where "id"=?]
sql args: [ ]
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type uuid: " "
my insert statement :-
INSERT INTO dbo.groups(
id, sortorder, name, hidden, sessionid, creationtime, modificationtime, regionid)
VALUES ('5A171D3F-F6A6-4D09-AE89-73B5793DA171', 1, 'abc', false, null,'2018-11-20 20:25:49.663', null, null);
my database table is :-
CREATE TABLE dbo.groups
(
id uuid NOT NULL,
sortorder integer NOT NULL DEFAULT 0,
name character varying(80) COLLATE pg_catalog."default",
hidden boolean NOT NULL DEFAULT false,
sessionid uuid,
creationtime timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
modificationtime timestamp without time zone,
regionid uuid,
CONSTRAINT "PK_dbo.Groups" PRIMARY KEY (id)
)
EDIT:
My source database is MS SQL Server and target database is Postgres
The value of UUID in this case is a space or tab as it could be seen from the error message
invalid input syntax for type uuid: " "
Try finding why instead of the concrete UUID value this space/tab value is passed

create tables from sql file on schema PostgreSQL

I try to create tables from file on my schema, but i receive error message. Users table created on public schema was successfully, but other table i created on test1 schema was wrong. I don't know why, help me please.
Thank for advance.
CREATE SCHEMA test1
--Create sequence
CREATE SEQUENCE test1.table_id_seq START 1;
--Create function to auto generate ID
CREATE OR REPLACE FUNCTION test1.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 1;
BEGIN
SELECT nextval('test1.table_id_seq') % 1024 INTO seq_id;
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
--Talbe ----users----
CREATE TABLE users
(
id bigserial NOT NULL PRIMARY KEY,
username varchar(30) NOT NULL UNIQUE,
password varchar NOT NULL,
first_name varchar(10),
last_name varchar(10),
profile_picture varchar,
create_time timestamp (0) without time zone
);
--Table ----photos----
CREATE TABLE test.photos
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
caption text,
low_resolution varchar,
hight_resolution varchar,
thumnail varchar,
user_id bigint NOT NULL REFERENCES users(id),
create_time timestamp (0) without time zone
--Table ----comments----
CREATE TABLE test1.comments
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
create_time timestamp (0) without time zone,
text text,
user_id bigint REFERENCES users(id),
photo_id bigint REFERENCES test1.photos(id)
);
--Table ----likes----
CREATE TABLE test1.likes
(
photo_id bigint REFERENCES test1.photos(id),
user_id bigint REFERENCES users(id),
);
--Table ----follows----
CREATE TABLE test1.follows
(
user_id bigint NOT NULL REFERENCES users(id),
target_id bigint NOT NULL REFERENCES users(id),
);
CREATE TABLE teset1.feeds
(
user_id bigint NOT NULL REFERENCES users(id),
photo_id bigint NOT NULL REFERENCES test1.photos(id),
create_time timestamp (0) without time zone,
);
Well, it would have been helpful for you to have shown us what errors you were getting and what you tried to do to fix them. But here are some obvious problems with the SQL you posted:
Missing semicolon after "CREATE SCHEMA test1 "
Missing closing paren and semicolon at the end of CREATE TABLE test.photos ...
Dump neglects to create the "test" schema where the "photos" table wants to go.
Several foreign key references of test1.photos.id, but "photos" was created in the "test" schema, not "test1".
Extraneous trailing commas after the last column definitions for several tables
Typo: "teset1.feeds", should be "test.feeds"
TL;DR Whoever created this dump file wasn't paying very close attention.
After fixing all the problems above, I managed to load your SQL.

How to restore public schema into different schema

I have a dump, where the data and the structure is in the public schema. I want to restore it into a schema with a custom name - how can I do that?
EDIT V 2:
My dump file is from heroku, and looks like this at the beginning:
PGDMP
!
pd6rq1i7f3kcath9.1.59.1.6<Y
0ENCODINENCODINGSET client_encoding = 'UTF8';
falseZ
00
STDSTRINGS
STDSTRINGS)SET standard_conforming_strings = 'off';
false[
126216385d6rq1i7f3kcatDATABASE?CREATE DATABASE d6rq1i7f3kcath WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
DROP DATABASE d6rq1i7f3kcath;
uc0lt9t3fj0da4false26152200publicSCHEMACREATE SCHEMA public;
DROP SCHEMA public;
postgresfalse\
SCHEMA publicCOMMENT6COMMENT ON SCHEMA public IS 'standard public schema';
postgresfalse5?307916392plpgsql EXTENSION?CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
DROP EXTENSION plpgsql;
false]
00EXTENSION plpgsqlCOMMENT#COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
false212?125516397_final_mode(anyarrayFUNCTION?CREATE FUNCTION _final_mode(anyarray) RETURNS anyelement
LANGUAGE sql IMMUTABLE
AS $_$
SELECT a
FROM unnest($1) a
GROUP BY 1
ORDER BY COUNT(1) DESC, 1
LIMIT 1;
$_$;
,DROP FUNCTION public._final_mode(anyarray);
publicuc0lt9t3fj0da4false5?125516398mode(anyelement) AGGREGATE?CREATE AGGREGATE mode(anyelement) (
SFUNC = array_append,
STYPE = anyarray,
INITCOND = '{}',
FINALFUNC = _final_mode
);
(DROP AGGREGATE public.mode(anyelement);
publicuc0lt9t3fj0da4false5224?125916399 advert_candidate_collector_failsTABLECREATE TABLE advert_candidate_collector_fails (
id integer NOT NULL,
advert_candidate_collector_status_id integer,
exception_message text,
stack_trace text,
url text,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
4DROP TABLE public.advert_candidate_collector_fails;
publicuc0lt9t3fj0da4false5?125916405'advert_candidate_collector_fails_id_seSEQUENCE?CREATE SEQUENCE advert_candidate_collector_fails_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
>DROP SEQUENCE public.advert_candidate_collector_fails_id_seq;
publicuc0lt9t3fj0da4false1615^
00'advert_candidate_collector_fails_id_seqSEQUENCE OWNED BYeALTER SEQUENCE advert_candidate_collector_fails_id_seq OWNED BY advert_candidate_collector_fails.id;
publicuc0lt9t3fj0da4false162_
00'advert_candidate_collector_fails_id_seq
SEQUENCE SETRSELECT pg_catalog.setval('advert_candidate_collector_fails_id_seq', 13641, true);
publicuc0lt9t3fj0da4false162?125916407#advert_candidate_collector_statusesTABLE?CREATE TABLE advert_candidate_collector_statuses (
id integer NOT NULL,
data_source_id character varying(120),
state character varying(15) DEFAULT 'Queued'::character varying,
source_name character varying(30),
collector_type character varying(30),
started_at timestamp without time zone,
ended_at timestamp without time zone,
times_failed integer DEFAULT 0,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
7DROP TABLE public.advert_candidate_collector_statuses;
publicuc0lt9t3fj0da4false240424055?125916412*advert_candidate_collector_statuses_id_seSEQUENCE?CREATE SEQUENCE advert_candidate_collector_statuses_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ADROP SEQUENCE public.advert_candidate_collector_statuses_id_seq;
publicuc0lt9t3fj0da4false1635`
00*advert_candidate_collector_statuses_id_seqSEQUENCE OWNED BYkALTER SEQUENCE advert_candidate_collector_statuses_id_seq OWNED BY advert_candidate_collector_statuses.id;
publicuc0lt9t3fj0da4false164a
00*advert_candidate_collector_statuses_id_seq
SEQUENCE SETVSELECT pg_catalog.setval('advert_candidate_collector_statuses_id_seq', 133212, true);
publicuc0lt9t3fj0da4false164?125916414advertsTABLE"CREATE TABLE adverts (
id integer NOT NULL,
car_id integer NOT NULL,
source_name character varying(20),
url text,
first_extraction timestamp without time zone,
last_observed_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone,
source_id character varying(255),
deactivated_at timestamp without time zone,
seller_id integer NOT NULL,
data_source_id character varying(100),
price integer,
availability_state character varying(15)
);
ROP TABLE public.adverts;
publicuc0lt9t3fj0da4false5?125916420adverts_id_seSEQUENCEpCREATE SEQUENCE adverts_id_seq
START WITH 1
INCREMENT BY 1
#Tometzky's solution isn't quite right, at least with 9.2's pg_dump. It'll create the table in the new schema, but pg_dump schema-qualifies the ALTER TABLE ... OWNER TO statements, so those will fail:
postgres=# CREATE DATABASE demo;
\cCREATE DATABASE
postgres=# \c demo
You are now connected to database "demo" as user "postgres".
demo=# CREATE TABLE public.test ( dummy text );
CREATE TABLE
demo=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
demo=# \q
$
$ pg_dump -U postgres -f demo.sql demo
$ sed -i 's/^SET search_path = public, pg_catalog;$/SET search_path = testschema, pg_catalog;/' demo.sql
$ grep testschema demo.sql
SET search_path = testschema, pg_catalog;
$ dropdb -U postgres demo
$ createdb -U postgres demo
$ psql -U postgres -c 'CREATE SCHEMA testschema;' demo
CREATE SCHEMA
$ psql -U postgres -f demo.sql -v ON_ERROR_STOP=1 -v QUIET=1 demo
psql:demo.sql:40: ERROR: relation "public.test" does not exist
$ psql demo
demo=> \d testschema.test
Table "testschema.test"
Column | Type | Modifiers
--------+------+-----------
dummy | text |
You will also need to edit the dump to remove the schema-qualification on public.test or change it to the new schema name. sed is a useful tool for this.
I could've sworn the correct way to do this was with pg_dump -Fc -n public -f dump.dbbackup then pg_restore into a new schema, but I can't seem to find out exactly how right now.
Update: Nope, it looks like sed is your best bet. See I want to restore the database with a different schema
Near the beginning of a dump file (created with pg_dump databasename) is a line:
SET search_path = public, pg_catalog;
Just change it to:
SET search_path = your_schema_name, pg_catalog;
Also you'll need to search for
ALTER TABLE public.
and replace with:
ALTER TABLE your_schema_name.