Julia - PostgreSQL に接続する

環境

Docker で PostgreSQL を立ち上げる

$ docker run -d --name postgres -e POSTGRES_PASSWORD=test -p 5432:5432 postgres:11.5

Julia から PostgreSQL に接続する

julia> using LibPQ, Tables

julia> conn = LibPQ.Connection("dbname=postgres host=127.0.0.1 user=postgres password=test port=5432")
PostgreSQL connection (CONNECTION_OK) with parameters:
  user = postgres
  password = ********************
  dbname = postgres
  host = 127.0.0.1
  port = 5432
  client_encoding = UTF8
  application_name = LibPQ.jl
  sslmode = prefer
  sslcompression = 1
  krbsrvname = postgres
  target_session_attrs = any

julia> execute(conn, "create table if not exists hoge (id int)")
PostgreSQL result

julia> execute(conn, "insert into hoge values (1)")
PostgreSQL result

julia> execute(conn, "insert into hoge values (2)")
PostgreSQL result

julia> result = execute(conn, "SELECT * FROM hoge")
PostgreSQL result

julia> rowtable(result)
2-element Array{NamedTuple{(:id,),Tuple{Union{Missing, Int32}}},1}:
 NamedTuple{(:id,),Tuple{Union{Missing, Int32}}}((1,))
 NamedTuple{(:id,),Tuple{Union{Missing, Int32}}}((2,))

julia> columntable(result)
(id = Union{Missing, Int32}[1, 2],)

github.com