lukasmartinelli/pgfutter
lukasmartinelli/pgfutter has 1,343 GitHub stars and 125 forks. GitHot measured 0 stars in 24 hours and -1 in 7 days. Last checked 2026-11-13 01:19 UTC. Last push 2020-09-02.
Import CSV and JSON into PostgreSQL the easy way
实测星标增长
- 24小时
- 0
- 7天
- -1
- 15天
- 正在收集历史
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文pgfutter


Import CSV and line delimited JSON into PostgreSQL the easy way. This small tool abstract all the hassles and swearing you normally have to deal with when you just want to dump some data into the database.
Features:
- Generated import tables (
pgfutter csv <file>and you're done) - Good performance using the
COPYstreaming protocol - Easy deployment
- Dealing with import errors
- Import over the network
- Only supports UTF8 encoding
Check out pgclimb for exporting data from PostgreSQL into different data formats.
Install
You can download a single binary for Linux, OSX or Windows.
OSX
wget -O pgfutter https://github.com/lukasmartinelli/pgfutter/releases/download/v1.2/pgfutter_darwin_amd64
chmod +x pgfutter
./pgfutter --help
Linux
wget -O pgfutter https://github.com/lukasmartinelli/pgfutter/releases/download/v1.2/pgfutter_linux_amd64
chmod +x pgfutter
./pgfutter --help
Install from source
go get github.com/lukasmartinelli/pgfutter
If you are using Windows or 32-bit architectures you need to download the appropriate binary yourself.
Import CSV
pgfutter will deal with CSV files conforming to RFC 4180.
Create friends.csv.
name,age,friends
Jacob,26,"Anthony"
Anthony,25,""
Emma,28,"Jacob,Anthony"
Import the CSV file.
pgfutter csv friends.csv
Because header rows are already provided pgfutter will create the appropriate
table and copy the rows.
| name | age | friends |
|---|---|---|
| Jacob | 26 | Anthony |
| Anthony | 25 | |
| Emma | 28 | Jacob,Anthony |
pgfutter will only help you to get the data into the database. After that
SQL is a great language to sanitize and normalize the data according to your desired database schema.
CREATE TABLE public.person (
name VARCHAR(200) PRIMARY KEY,
age INTEGER
)
CREATE TABLE public.friendship (
person VARCHAR(200) REFERENCES public.person(name),
friend VARCHAR(200) REFERENCES public.person(name)
)
INSERT INTO public.person
SELECT name, age::int
FROM import.friends
WITH friends AS
(SELECT name as person, regexp_split_to_table(friends, E'\\,') AS friend
FROM import.friends)
INSERT INTO public.friendship
SELECT * FROM
friends WHERE friend <> ''
Import JSON
A lot of event logs contain JSON objects nowadays (e.g. GitHub Archive).
pgfutter expects each line to have a valid JSON object. Importing JSON is only supported for Postgres 9.3 and Postgres 9.4 due to the JSON type.
Create friends.json.
{"name": "Jacob", "age": 26, "friends": ["Anthony"]}
{"name": "Anthony", "age": 25, "friends": []}
{"name": "Emma", "age": 28, "friends": ["Jacob", "Anthony"]}
Import the JSON file.
pgfutter json friends.json
Your JSON objects will be stored in a single JSON column called data.
| data |
|---|
{"name": "Jacob", "age": 26, "friends": ["Anthony"]} |
{"name": "Anthony", "age": 25, "friends": []} |
{"name": "Emma", "age": 28, "friends": ["Jacob", "Anthony"]} |
PostgreSQL has excellent JSON support which means you can then start normalizing your data.
CREATE TABLE public.person (
name VARCHAR(200) PRIMARY KEY,
age INTEGER
)
CREATE TABLE public.friendship (
person VARCHAR(200) REFERENCES public.person(name),
friend VARCHAR(200) REFERENCES public.person(name)
)
INSERT INTO public.person
SELECT data->>'name' as name, (data->>'age')::int as age
FROM import.friends
INSERT INTO public.friendship
SELECT data->>'name' as person, json_array_elements_text(data->'friends')
FROM import.friends
Database Connection
Database connection details can be provided via environment variables or as separate flags.
| name | default | description |
|---|---|---|
DB_NAME |
postgres |
database name |
DB_HOST |
localhost |
host name |
DB_PORT |
5432 |
port |
DB_SCHEMA |
import |
schema to create tables for |
DB_USER |
postgres |
database user |
DB_PASS |
password (or empty if none) |
Advanced Use Cases
Custom delimiter
Quite often you want to specify a custom delimiter (default: ,).
pgfutter csv -d "\t" traffic_violations.csv
You have to use " as a quoting character and \ as escape character.
You might omit the quoting character if it is not necessary.
Using TAB as delimiter
If you want to use tab as delimiter you need to pass $'\t' as delimiter
to ensure your shell does not swallow the correct delimiter.
pgfutter csv -d $'\t' traffic_violations.csv
Custom header fields
If you want to specify the field names explicitly you can skip the header row and pass a comma separated field name list.
pgfutter csv --skip-header --fields "name,state,year" traffic_violations.csv
If you don't have a header row in a document you should specify the field names as well.
pgfutter csv --fields "name,state,year" traffic_violations.csv
Encoding
All CSV files need to be utf-8 encoded. No other encoding is supported.
Encoding is a nasty topic and you should deal with it before it enters
the database.
Dealing with invalid input
A lot of CSV files don't confirm to proper CSV standards. If you want
to ignore errors you can pass the --ignore-errors flag which will
commit the transaction even if some rows cannot be imported.
The failed rows will be written to stdout so you can clean them up with other tools.
pgfutter --ignore-errors csv traffic_violations.csv 2> traffic_violations_errors.csv
This works the same for invalid JSON objects.
Custom Table
pgfutter will take the sanitized filename as the table name. If you want to specify a custom table name or import into your predefined table schema you can specify the table explicitly.
pgfutter --table violations csv traffic_violations.csv
Alternatives
For more sophisticated needs you should take a look at pgloader.
Regression Tests
The program is tested with open data sets from around the world.
Download all samples into the folder samples.
./download-samples.sh
Run import regression tests against the samples.
./test.sh
Cross-compiling
We use gox to create distributable binaries for Windows, OSX and Linux.
docker run --rm -v "$(pwd)":/usr/src/pgfutter -w /usr/src/pgfutter tcnksm/gox:1.9
探索相关项目
Asabeneh/30-Days-Of-Python
30 Days of Python 编程挑战是一份循序渐进的指南,用于在 30 天内学习 Python 编程语言。这项挑战可能需要超过 100 天。请按自己的节奏进行。这些视频或许也有帮助:https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,618 星标grafana/grafana
开放且可组合的可观测性与数据可视化平台。可对来自 Prometheus、Loki、Elasticsearch、InfluxDB、Postgres 等众多数据源的指标、日志和追踪进行可视化。
77,859 星标redis/redis
对于正在构建实时数据驱动应用的开发者而言,Redis 是首选、速度最快且功能最丰富的缓存、数据结构服务器和文档与向量查询引擎。
77,472 星标apache/superset
Apache Superset 是一个数据可视化与数据探索平台
75,495 星标