# Create a PostgreSQL Table from a DataFrame or CSV with Schema Inference

**URL:** <https://discourse.julialang.org/t/create-a-postgresql-table-from-a-dataframe-or-csv-with-schema-inference/77328>\
**Category:** New to Julia\
**Tags:** question, dataframes, postgresql\
**Created:** [March 2, 2022, 11:49pm UTC](https://discourse.julialang.org/t/create-a-postgresql-table-from-a-dataframe-or-csv-with-schema-inference/77328 "2022-03-02T23:49:32Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 3, 2022, 3:06am UTC](https://discourse.julialang.org/t/create-a-postgresql-table-from-a-dataframe-or-csv-with-schema-inference/77328/2 "2022-03-03T03:06:06Z")

</div>

Welcome to Julia discourse!

Here is a work-around that uses an in-memory SQLite database to generate the schema:

```julia
using DataFrames, SQLite, LibPQ
# An example test dataframe:
df = DataFrame(A = 1:3, B = [2.0, -1.1, 2.8], C = ["p","q","r"])
# Get the table schema:
table_sch = Tables.schema(df)
# Create an in-memory SQLite database:
db = SQLite.DB()
# Just create the schema in the SQLite database:
SQLite.createtable!(db, "mytable", Tables.schema(df))
# Now get back the generated SQL CREATE statement:
res = DataFrame(DBInterface.execute(db, "SELECT sql FROM sqlite_master;"))
# alternative to just get a specific name:
# res = DataFrame(DBInterface.execute(db, "SELECT * FROM sqlite_master WHERE name='mytable';"))

str = res[1,:sql] # or change the first index of `1` to other indices (e.g for multiple tables)
# Now use the string to create a table in LibPQ:
conn = LibPQ.Connection("dbname=postgres") # replace postgres with your db name.
result = LibPQ.execute(conn, str)

```

and from here, you can use the function from this post

> [@How to Create a Table in a DataBase Using DataFrames?](https://discourse.julialang.org/t/how-to-create-a-table-in-a-database-using-dataframes/75759/3):
>
> 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!) 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 …

to load the table into the database:

```julia
load_table!(conn, df, "mytable")

```

---

_[View the full topic](https://discourse.julialang.org/t/create-a-postgresql-table-from-a-dataframe-or-csv-with-schema-inference/77328)._
