Can I automatically create a table in PostgreSQL from a csv file with headers?

前端 未结 8 749
一整个雨季
一整个雨季 2020-11-30 00:22

I\'m running PostgreSQL 9.2.6 on OS X 10.6.8. I would like to import data from a CSV file with column headers into a database. I can do this with the COPY state

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 00:50

    There is a very good tool that imports tables into Postgres from a csv file. It is a command-line tool called pgfutter (with binaries for windows, linux, etc.). One of its big advantages is that it recognizes the attribute/column names as well.

    The usage of the tool is simple. For example if you'd like to import myCSVfile.csv:

    pgfutter --db "myDatabase" --port "5432" --user "postgres" --pw "mySecretPassword" csv myCSVfile.csv
    

    This will create a table (called myCSVfile) with the column names taken from the csv file's header. Additionally the data types will be identified from the existing data.

    A few notes: The command pgfutter varies depending on the binary you use, e.g. it could be pgfutter_windows_amd64.exe (rename it if you intend to use this command frequently). The above command has to be executed in a command line window (e.g. in Windows run cmd and ensure pgfutter is accessible). If you'd like to have a different table name add --table "myTable"; to select a particular database schema us --schema "mySchema". In case you are accessing an external database use --host "myHostDomain".

    A more elaborate example of pgfutter to import myFile into myTable is this one:

    pgfutter --host "localhost" --port "5432" --db "myDB" --schema "public" --table "myTable" --user "postgres" --pw "myPwd" csv myFile.csv
    

    Most likely you will change a few data types (from text to numeric) after the import:

    alter table myTable
      alter column myColumn type numeric
        using (trim(myColumn)::numeric)
    

提交回复
热议问题