Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:58:01+00:00 2026-06-10T19:58:01+00:00

I’m trying to setup temporary tables for unit-testing purposes. So far I managed to

  • 0

I’m trying to setup temporary tables for unit-testing purposes. So far I managed to create a temporary table which copies the structure of an existing table:

CREATE TEMP TABLE t_mytable (LIKE mytable INCLUDING DEFAULTS);

But this lacks the data from the original table. I can copy the data into the temporary table by using a CREATE TABLE AS statement instead:

CREATE TEMP TABLE t_mytable AS SELECT * FROM mytable;

But then the structure of t_mytable will not be identical, e.g. column sizes and default values are different. Is there a single statement which copies everything?

Another problem with the first query using LIKE is that the key column still references the SEQUENCE of the original table, and thus increments it on insertion. Is there an easy way to create the new table with its own sequence, or will I have to set up a new sequence by hand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:58:03+00:00Added an answer on June 10, 2026 at 7:58 pm

    Postgres 10 or later

    Postgres 10 introduced IDENTITY columns conforming to the SQL standard (with minor extensions). The ID column of your table would look something like:

    id    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
    

    Syntax in the manual.
    Using this instead of a traditional serial column avoids your problem with sequences. IDENTITY columns use exclusive, dedicated sequences automatically, even when the specification is copied with LIKE. The manual:

    Any identity specifications of copied column definitions will only be
    copied if INCLUDING IDENTITY is specified. A new sequence is created
    for each identity column of the new table, separate from the sequences
    associated with the old table.

    And:

    INCLUDING ALL is an abbreviated form of INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS.

    The solution is simpler now:

    CREATE TEMP TABLE t_mytable (LIKE mytable INCLUDING ALL);
    INSERT INTO t_mytable TABLE mytable;
    SELECT setval(pg_get_serial_sequence('t_mytable', 'id'), max(id)) FROM tbl;
    

    As demonstrated, you can still use setval() to set the sequence’s current value. A single SELECT does the trick. pg_get_serial_sequence()]6 gets the name of the sequence.

    db<>fiddle here

    Related:

    • How to reset postgres' primary key sequence when it falls out of sync?
    • Is there a shortcut for SELECT * FROM?
    • Creating a PostgreSQL sequence to a field (which is not the ID of the record)

    Original (old) answer

    You can take the create script from a database dump or a GUI like pgAdmin (which reverse-engineers database object creation scripts), create an identical copy (with separate sequence for the serial column), and then run:

    INSERT INTO new_tbl
    SELECT * FROM old_tbl;
    

    The copy cannot be 100% identical if both tables reside in the same schema. Obviously, the table name has to be different. Index names would conflict, too. Retrieving serial numbers from the same sequence would probably not be in your best interest, either. So you have to (at least) adjust the names.

    Placing the copy in a different schema avoids all of these conflicts. While you create a temporary table from a regular table like you demonstrated, that’s automatically the case since temp tables reside in their own temporary schema.

    Or look at Francisco’s answer for DDL code to copy directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.