HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

PostgreSQL 9.2; Altering a table to use another sequence

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlsequenceanotheralteringusetable

Problem

I've created a table like so:

create table doki_data_item2 (like doki_data_item including defaults);


but this new table uses the same sequence as the old table.

So two questions:

  • How can I copy and table and the necessary sequences easily?



  • How can I replace a sequence on one table with another sequence?

Solution

Why don't you create a new sequence:

create sequence new_sequence;

Then create the new table

create table doki_data_item2 (like doki_data_item including defaults);

Finally modify the column that is using the old sequence to use the new one:

alter table doki_data_item2 alter column seq_column set default nextval('new_sequence');

You can obviously set various parameters for the newly created sequence to suit your needs.

Context

StackExchange Database Administrators Q#59610, answer score: 9

Revisions (0)

No revisions yet.