Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a copy of table in PostgreSQL?

Tags:

sql

postgresql

I want to save a copy of specific table with it's data. All I care is the structure and data. I don't want to copy the keys nor constrains.

I read this answer Copy table structure into new table but it's not quite what I need.

How it can be done?

like image 486
avi Avatar asked Apr 06 '17 07:04

avi


People also ask

What is PostgreSQL copy command?

Using the \copy command to import data to a table on a PostgreSQL DB instance. The PostgreSQL \copy command is a meta-command available from the psql interactive client tool. You can use \copy to import data into a table on your RDS for PostgreSQL DB instance.

Does Postgres COPY overwrite?

If you COPY data into a table already containing data, the new data will be appended. If you COPY TO a file already containing data, the existing data will be overwritten.


1 Answers

You are probably looking for CREATE TABLE AS SELECT, e.g.:

CREATE TABLE copy_table AS SELECT * FROM original_table;
like image 119
Vao Tsun Avatar answered Sep 28 '22 17:09

Vao Tsun