using LibPQ, Tables
conn = LibPQ.Connection("dbname=postgres")
result = execute(conn, "SELECT typname FROM pg_type WHERE oid = 16")
data = columntable(result)
# the same but with parameters
result = execute(conn, "SELECT typname FROM pg_type WHERE oid = \$1", ["16"])
data = columntable(result)
# the same but asynchronously
async_result = async_execute(conn, "SELECT typname FROM pg_type WHERE oid = \$1", ["16"])
# do other things
result = fetch(async_result)
data = columntable(result)
close(conn)using LibPQ
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER")
result = execute(conn, """
CREATE TEMPORARY TABLE libpqjl_test (
no_nulls varchar(10) PRIMARY KEY,
yes_nulls varchar(10)
);
""")
LibPQ.load!(
(no_nulls = ["foo", "baz"], yes_nulls = ["bar", missing]),
conn,
"INSERT INTO libpqjl_test (no_nulls, yes_nulls) VALUES (\$1, \$2);",
)
using DataFrames
no_nulls = map(string, 'a':'z')
yes_nulls = Union{String, Missing}[isodd(Int(c)) ? string(c) : missing for c in 'a':'z']
data = DataFrame(no_nulls=no_nulls, yes_nulls=yes_nulls)
execute(conn, "DELETE FROM libpqjl_test;")
using CSV
"""
Function for upload of a Tables.jl compatible data structure (e.g. DataFrames.jl) into the db.
"""
function load_by_copy!(table, con:: LibPQ.Connection, tablename:: AbstractString)
iter = CSV.RowWriter(table)
column_names = first(iter)
copyin = LibPQ.CopyIn("COPY $tablename ($column_names) FROM STDIN (FORMAT CSV, HEADER);", iter)
execute(con, copyin)
end
load_by_copy!(data, conn, "libpqjl_test")
close(conn)When inserting a large number of rows, wrapping your insert queries in a transaction will greatly increase performance. See the PostgreSQL documentation 14.4.1. Disable Autocommit for more information.
Concretely, this means surrounding your query like this:
execute(conn, "BEGIN;")
LibPQ.load!(
(no_nulls = ["foo", "baz"], yes_nulls = ["bar", missing]),
conn,
"INSERT INTO libpqjl_test (no_nulls, yes_nulls) VALUES (\$1, \$2);",
)
execute(conn, "COMMIT;")An alternative to repeated INSERT queries is the PostgreSQL COPY query.
LibPQ.CopyIn makes it easier to stream data to the server using a COPY FROM STDIN query.
using LibPQ, DataFrames
conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER")
row_strings = imap(eachrow(df)) do row
if ismissing(row[:yes_nulls])
"$(row[:no_nulls]),\n"
else
"$(row[:no_nulls]),$(row[:yes_nulls])\n"
end
end
copyin = LibPQ.CopyIn("COPY libpqjl_test FROM STDIN (FORMAT CSV);", row_strings)
execute(conn, copyin)
close(conn)