# 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:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [February 3, 2022, 9:01pm UTC](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759/1 "2022-02-03T21:01:16Z")

</div>

Hi all!

I tried looking through the documentation, but I could not find it readily - is there a way to write a DataFrame to either a Postgresql (via LibPQ.jl) or MySQL (via MySQL.jl) database as a new table?  
Ideally, it would be nice to have a command like:

```julia
exec(connection = conn, sql = """CREATE TABLE my_dataframe 
                               AS SELECT $(df)"""

```

Or something of the sort.  
Any thoughts?  
Thanks!

~ tcp 🌳

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [February 3, 2022, 9:19pm UTC](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759/2 "2022-02-03T21:19:39Z")

</div>

You can checkout `MySQL.load`:

> MySQL.jl attempts to provide a convenient `MySQL.load(table, conn, table_name)` function for generically loading Tables.jl-compatible sources into database tables. While the mysql API has some utilities for even making this possible, just note that it can be tricky to do generically in practice due to specific modifications needed in `CREATE TABLE` and column type statements.

---

<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

```

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [February 4, 2022, 2:34pm UTC](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759/4 "2022-02-04T14:34:40Z")

</div>

How would you expect String columns to be defined ?

`varchar(maximum(map(length, df[!, :txt])))`
