Withdata Software

Import data from TXT to PostgreSQL via command line on MacOS

Want to import data from TXT file to PostgreSQL via Command Line Interface (CLI) on MacOS?

Using pg-cmd, a Command Line Interface (CLI) client for PostgreSQL, you can import data from TXT file to PostgreSQL easily and fast. Direct Interaction or Scripting and Automation.

Download pg-cmd Macos version
pg-cmd.darwin-amd64.zip
pg-cmd.darwin-arm64.zip

Import data from TXT file to PostgreSQL via command line on MacOS

1. In interactive mode:

pg-cmd> load loadtype=replace
      > table=media_messages 
      > datafile=/data/media_messages.txt 
      > delimiter=, 
      > quote=" 
      > header=y;

2. In batch mode:

pg-cmd dsn="pan:withdata@192.168.0.102/test" task=load loadtype=replace table=media_messages datafile=/data/media_messages.txt delimiter=, quote=" header=y quit=y

“loadtype=replace” will delete all data in table before inserting.

See parameters detail.


More about pg-cmd – Command Line Interface (CLI) tool for PostgreSQL

Download pg-cmd Macos version
pg-cmd.darwin-amd64.zip
pg-cmd.darwin-arm64.zip

TXT → PostgreSQL reference

Type mapping

Value kindin TXTin PostgreSQL
Int1234, or right-aligned in a fixed-width columninteger
Bigint9007199254740993bigint
Decimal1234.56numeric(p,s)
Float1.234e+10double precision
Textwritten as-is; padding depends on the chosen layoutvarchar(n)
Long textwritten as-istext
Datewhatever the writer chosedate
Datetimewhatever the writer chosetimestamp
Binarynot representablebytea
BooleanY / N, or 1 / 0, or TRUE / FALSEboolean
JSONwritten as-isjsonb

Dialect differences

TXT is a file format and PostgreSQL is a database server, so the target has to be told the types the file never carried - the rows below are where the two sides diverge.

Propertyin TXTin PostgreSQL
port—5432
identifiersNone by convention"double quotes"
row limit—LIMIT n
datesWhatever the writer produced; no convention is guaranteedtimestamp, timestamptz, date, time
long text—TEXT
binaryNot supportedBYTEA
separatorWhatever was chosen - often fixed column widths—
encodingNo declaration - UTF-8 works only with a BOM—
typingNone - every field is text—
shapeFree form; a parser has to be told the layoutTables of rows and columns
ceilingNo formal limit—

Command line

SideCommand
PostgreSQLpg_dump or COPY ... TO
PostgreSQL (load)COPY ... FROM

Convert your data online, free

Free in-browser converters for CSV, Excel, JSON, XML and SQL: Withdata Online Data Conversion. Large files, batch conversion or scheduling? See the offline desktop apps, or browse all data conversion tools.