Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Don't copy all rows in postgres

I want to copy some data from a table to another but I dont want to copy all the rows, just a few a them (like only the first 100).

I didn't find an option in the COPY command for this. So that's only possible or not ? Thank you in advance.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You didn't look very hard.

https://www.postgresql.org/docs/current/static/sql-copy.html

To copy into a file just the countries whose names start with 'A':

COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/usr1/proj/bray/sql/a_list_countries.copy';

So you would want COPY (SELECT ... LIMIT 100).

I'm assuming you want to copy between databases or via an intermediate file or something, otherwise you just want to use INSERT ... SELECT as shown in another answer.

over 4 years ago · Santiago Trujillo Report

0

You can use

INSERT INTO table2(col_list) 
SELECT col_list FROM table1 WHERE condtitions ORDER BY order_col_list LIMIT X;
over 4 years ago · Santiago Trujillo Report

0

You can limit number of rows when you copy (SELECT ...LIMIT) to, but not COPY from. You did not find such options in docs, because there are no such.

use insert into ... select from ... limit 100 instead

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!