How to upload csv file to postgres SQL

 When a spreadsheet is given to you to upload to SQL, you can follow this procedure.

First convert spreadsheet to .csv (comma delimited) file. Just open the file and save as csv (comma delimited) file.

Next, check how many columns are there in your spreadsheet and then create those column names manually in SQL by following this process.


We have created instacart database here. Under Schemas, we can see tables. Right click and select CREATE TABLE option.

Just enter table name that you want to create. We have used here table name as orders.  Do not click SAVE yet.


Before you click SAVE, click on columns from top menu bar. Using plus sign on the right, you can now add more columns one by one.



Now we have created all columns one by one. Just remember that the names of the columns should match exactly same as in csv file. Otherwise while uploading data, you will keep on getting errors.



Another thing is, there are options to set up column constraints by activating NULL and Primary Key options. Null option will prevent any null values in the columns and primary key will allow only unique and non-null values in the column.

Do this ONLY IF you are sure that the data in csv file is cleaned. Otherwise do not activate these constraints because you will keep on getting errors while uploading csv file.

Now you can click SAVE at the bottom of the page. Our table structure is ready.

Next, confirm it by running the SELECT command. We can see all columns created correctly but there is no data yet.

Now, we will upload data from csv file that is saved to our computer to this table.




Right click on orders and select import/export option



Select following options from import/export page.

  • Turn the option to import
  • Give the location of your order.csv file on your computer
  • Select the format as csv
  • If your csv file has header row displaying column names then select header as Yes
  • And select delimiter as " , " because our csv file is comma delimited file.



Once you click OK, it is now uploading the csv file and you will get confirmation message once it is done.


Click on more details to see how many rows uploaded in how much time


It is showing 3.4 million rows uploaded in 16.93 seconds. Check your csv file and confirm if it has same number of rows then all data is uploaded. If not check the data in csv file for any issues.

Now we can view the data using SELECT command




No comments:

Post a Comment

Complex query example

Requirement:  You are given the table with titles of recipes from a cookbook and their page numbers. You are asked to represent how the reci...