# How to Create a Table in a DataBase Using DataFrames?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759>\
**Category:** Data\
**Tags:** data, dataframes, database\
**Created:** [February 3, 2022, 9:01pm UTC](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759 "2022-02-03T21:01:15Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![chris-b1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-b1/32/14165_2.png) [@chris-b1](https://discourse.julialang.org/u/chris-b1)\
**Post date:** [February 3, 2022, 9:53pm UTC](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759/3 "2022-02-03T21:53:36Z")

</div>

LibPQ itself doesn’t provider a wrapper for writing tables, here’s an example one that I’ve written (with limited testing, so please use at your own caution!)

```julia
function load_table!(conn, df, tablename, columns=names(df))
    table_column_names = join(string.(columns), ", ")
    placeholders = join(("\$$num" for num in 1:length(columns)), ", ")
    data = select(df, columns)
    try
        LibPQ.execute(conn, "BEGIN;")
        LibPQ.load!(
            data,
            conn,
            "INSERT INTO $tablename ($(table_column_names)) VALUES ($placeholders)"
        )
        LibPQ.execute(conn, "COMMIT;")
    catch
        LibPQ.execute(conn, "ROLLBACK;")
    end
end

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759)._
