# LibPQ.jl Insertion Example Does Not Work (Windows 2012)

**URL:** https://discourse.julialang.org/t/libpq-jl-insertion-example-does-not-work-windows-2012/15629
**Category:** New to Julia
**Created:** [September 28, 2018, 5:09pm UTC](https://discourse.julialang.org/t/libpq-jl-insertion-example-does-not-work-windows-2012/15629 "2018-09-28T17:09:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jort](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jort](https://discourse.julialang.org/u/jort)
#### Post date: [September 28, 2018, 5:09pm UTC](https://discourse.julialang.org/t/libpq-jl-insertion-example-does-not-work-windows-2012/15629/1 "2018-09-28T17:09:29Z")

</div>

Hello, new to Julia, so please excuse my ignorance. Probably I should be able to figure out how to use LibPQ from the rest of the documentation, but I cannot even get the verbatim example (save for unique database connection information) of a data insertion to work.

Working with Julia 1.0.0 under Atom (Junolab) on a Windows 2012 R2 server.

## So this example taken from [https://invenia.github.io/LibPQ.jl/stable/index.html](https://invenia.github.io/LibPQ.jl/stable/index.html) fails:

using LibPQ, DataStreams

conn = LibPQ.Connection(“dbname=postgres user=postgres password=XXXXXXXXXXXX”)

result = execute(conn, “”"  
CREATE TEMPORARY TABLE libpqjl\_test (  
no\_nulls varchar(10) PRIMARY KEY,  
yes\_nulls varchar(10)  
);  
“”")

Data.stream!(  
data,  
LibPQ.Statement,  
conn,  
“INSERT INTO libpqjl\_test (no\_nulls, yes\_nulls) VALUES ($1, $2);”,  
)

## close(conn)

with what seems an obvious error:

Press Enter to start Julia.  
Starting Julia…  
\_  
\_ \_ _(_)\_ | Documentation: [https://docs.julialang.org](https://docs.julialang.org)  
(_) | (_) (_) |  
\_ \_ | | \_\_ \_ | Type “?” for help, “]?” for Pkg help.  
| | | | | | |/ ` | |  
| | || | | | (_| | | Version 1.0.0 (2018-08-08)  
_/ |\ __‘_|_|_|\__ ’\_| | Official [https://julialang.org/](https://julialang.org/) release  
|\_\_/ |

## [info | LibPQ]: CREATE TABLE ERROR: LoadError: UndefVarError: data not defined Stacktrace: [1] top-level scope at none:0 [2] include\_string(::Module, ::String, ::String) at .\loading.jl:1002 [3] (::getfield(Atom, Symbol(“##118#123”)){String,String,Module})() at C:\Users\jort.julia\packages\Atom\WSz3k\src\eval.jl:120 [4] withpath(::getfield(Atom, Symbol(“##118#123”)){String,String,Module}, ::String) at C:\Users\jort.julia\packages\CodeTools\hB4Hy\src\utils.jl:30 [5] withpath at C:\Users\jort.julia\packages\Atom\WSz3k\src\eval.jl:46 [inlined] [6] #117 at C:\Users\jort.julia\packages\Atom\WSz3k\src\eval.jl:117 [inlined] [7] hideprompt(::getfield(Atom, Symbol(“##117#122”)){String,String,Module}) at C:\Users\jort.julia\packages\Atom\WSz3k\src\repl.jl:76 [8] macro expansion at C:\Users\jort.julia\packages\Atom\WSz3k\src\eval.jl:116 [inlined] [9] (::getfield(Atom, Symbol(“##116#121”)){Dict{String,Any}})() at .\task.jl:85 in expression starting at H:\Julia\_Projects\ODM\libpq\_test.jl:12

since “data” is not defined anywhere. How should it be defined so that it works with the parameters in the INSERT statement? I’ve been unable to find a clear example of LibPQ usage anywhere.

Many thanks!

---

<div class="post-metadata">

### Author: ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)
#### Post date: [September 29, 2018, 1:21pm UTC](https://discourse.julialang.org/t/libpq-jl-insertion-example-does-not-work-windows-2012/15629/2 "2018-09-29T13:21:14Z")

</div>

> since “data” is not defined anywhere.

As I see - maybe the `data` is defined in the previous example.  
imho: Not a beginner friendly.  
You can give a feedback here: [GitHub - iamed2/LibPQ.jl: A Julia wrapper for libpq](https://github.com/invenia/LibPQ.jl)

**My similar - working example - maybe you can use - Run Step by step - and compare the logs**

```julia
using LibPQ, DataStreams , DataFrames
conn = LibPQ.Connection("dbname=$(ENV["PGDATABASE"]) user=$(ENV["PGUSER"])")

result = execute(conn, """
    DROP TABLE IF EXISTS libpqjl_test;
    CREATE TABLE libpqjl_test (
        lang TEXT PRIMARY KEY,
        web TEXT
    );
""")

langs = DataFrame( langv =["Julia", "Go", "Rust"],
                   webv =["julialang.org", "golang.org", "www.rust-lang.org"]);

langs

Data.stream!(
    langs ,
    LibPQ.Statement,
    conn,
    "INSERT INTO libpqjl_test (lang, web ) VALUES (\$1, \$2);"
);

close(conn)

```

**logs**

```julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.0 (2018-08-08)
_/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using LibPQ, DataStreams , DataFrames

julia> conn = LibPQ.Connection("dbname=$(ENV["PGDATABASE"]) user=$(ENV["PGUSER"])")
PostgreSQL connection (CONNECTION_OK) with parameters:
user = *******
password = ********************
dbname = *******
host = db
port = 5432
client_encoding = UTF8
application_name = LibPQ.jl
sslmode = prefer
sslcompression = 1
krbsrvname = postgres
target_session_attrs = any

julia> result = execute(conn, """
        DROP TABLE IF EXISTS libpqjl_test;
        CREATE TABLE libpqjl_test (
            lang TEXT PRIMARY KEY,
            web TEXT
        );
    """)
[info | LibPQ]: CREATE TABLE
PostgreSQL result

julia> langs = DataFrame( langv =["Julia", "Go", "Rust"],
                       webv =["julialang.org", "golang.org", "www.rust-lang.org"]);

julia> langs
3×2 DataFrame
│ Row │ langv │ webv │
│ │ String │ String │
├─────┼────────┼───────────────────┤
│ 1 │ Julia │ julialang.org │
│ 2 │ Go │ golang.org │
│ 3 │ Rust │ www.rust-lang.org │

julia> Data.stream!(
        langs ,
        LibPQ.Statement,
        conn,
        "INSERT INTO libpqjl_test (lang, web ) VALUES (\$1, \$2);"
    );
[info | LibPQ]: 
[info | LibPQ]: 
[info | LibPQ]: INSERT 0 1
[info | LibPQ]: INSERT 0 1
[info | LibPQ]: INSERT 0 1

julia> close(conn)

julia> 

```

---

<div class="post-metadata">

### Author: ![jort](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jort](https://discourse.julialang.org/u/jort)
#### Post date: [November 15, 2018, 4:35pm UTC](https://discourse.julialang.org/t/libpq-jl-insertion-example-does-not-work-windows-2012/15629/3 "2018-11-15T16:35:19Z")

</div>

Got it to work, thank you!
