How to import CSV file data into a PostgreSQL table?

前端 未结 19 2673
再見小時候
再見小時候 2020-11-22 02:14

How can I write a stored procedure that imports data from a CSV file and populates the table?

19条回答
  •  没有蜡笔的小新
    2020-11-22 03:13

    How to import CSV file data into a PostgreSQL table?

    steps:

    1. Need to connect postgresql database in terminal

      psql -U postgres -h localhost
      
    2. Need to create database

      create database mydb;
      
    3. Need to create user

      create user siva with password 'mypass';
      
    4. Connect with database

      \c mydb;
      
    5. Need to create schema

      create schema trip;
      
    6. Need to create table

      create table trip.test(VendorID int,passenger_count int,trip_distance decimal,RatecodeID int,store_and_fwd_flag varchar,PULocationID int,DOLocationID int,payment_type decimal,fare_amount decimal,extra decimal,mta_tax decimal,tip_amount decimal,tolls_amount int,improvement_surcharge decimal,total_amount
      );
      
    7. Import csv file data to postgresql

      COPY trip.test(VendorID int,passenger_count int,trip_distance decimal,RatecodeID int,store_and_fwd_flag varchar,PULocationID int,DOLocationID int,payment_type decimal,fare_amount decimal,extra decimal,mta_tax decimal,tip_amount decimal,tolls_amount int,improvement_surcharge decimal,total_amount) FROM '/home/Documents/trip.csv' DELIMITER ',' CSV HEADER;
      
    8. Find the given table data

      select * from trip.test;
      

提交回复
热议问题