How to export some fields with their records to a new postgres table

-1

If I had a table with n fields and m records and I want to create a new table with some fields and all the records, understanding that there are fields that image images among other formats, how can I import those records and fields in the most optimal way and then you can delete the old table. Is it possible to create a method that does those procedures optimally if they are a considerable amount of records?.

    
asked by García Henry 30.10.2017 в 18:03
source

2 answers

2

I would do it directly in Postgresql (outside Rails ), a simple INSERT INTO ... SELECT is enough.

For example, assuming I have a table called origen with the fields campo1 , campo2 , campo3 and campo4 but I only want the fields campo1 and campo3 in a new table, called destino 1 , then I would use the following query :

INSERT INTO destino (campo1, campo3)
SELECT campo1, campo3 FROM origen;

1 It is assumed that the destino table has already been created.

    
answered by 30.10.2017 / 18:20
source
1

is brief in postgresql it would simply be

create table tu_tabla_destino as select campo1,campo2 from tu_tabla_origen;
    
answered by 15.11.2017 в 17:12