How to copy table in PostgeSQL Database

 How to copy table in PostgeSQL Database


db1-# \dt

            List of relations

 Schema |     Name     | Type  |  Owner

--------+--------------+-------+----------

 public | bookings     | table | postgres

 public | feb_bookings | table | postgres

 public | jan_bookings | table | postgres

(3 rows)


Create table with data

db1=# create table copy_bookings as table bookings;

SELECT 3

db1=#

db1=#

db1=# /dt

db1-# \dt

             List of relations

 Schema |     Name      | Type  |  Owner

--------+---------------+-------+----------

 public | bookings      | table | postgres

 public | copy_bookings | table | postgres

 public | feb_bookings  | table | postgres

 public | jan_bookings  | table | postgres

(4 rows)


Create table with nodata

db1=# create table nodata_bookings as table bookings with NO DATA;

CREATE TABLE AS

db1=#

db1=#

db1=# \dt

              List of relations

 Schema |      Name       | Type  |  Owner

--------+-----------------+-------+----------

 public | bookings        | table | postgres

 public | copy_bookings   | table | postgres

 public | feb_bookings    | table | postgres

 public | jan_bookings    | table | postgres

 public | nodata_bookings | table | postgres

(5 rows)


db1=# select * from nodata_bookings;

 flightno | flightname | booking_date

----------+------------+--------------

(0 rows)


db1=# select * from copy_bookings;

 flightno | flightname |    booking_date

----------+------------+---------------------

 auh345   | etihad     | 2024-01-10 00:00:00

 dxb102   | emirates   | 2024-02-09 00:00:00

 dxb103   | emirates   | 2024-02-15 00:00:00

(3 rows)


Comments