# Psycopg2 on PBS hook

**URL:** <https://community.openpbs.org/t/psycopg2-on-pbs-hook/1012>\
**Category:** Users/Site Administrators\
**Created:** [May 30, 2018, 4:39am UTC](https://community.openpbs.org/t/psycopg2-on-pbs-hook/1012 "2018-05-30T04:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimloai](https://avatars.discourse-cdn.com/v4/letter/k/4af34b/32.png) [@kimloai](https://community.openpbs.org/u/kimloai)\
**Post date:** [May 30, 2018, 4:39am UTC](https://community.openpbs.org/t/psycopg2-on-pbs-hook/1012/1 "2018-05-30T04:39:40Z")

</div>

Hi all,

I’ve tried to use PBS hook and psycopg2 python library to write jobs’ data and users’ data to postgres database.

The problem is when I let the event trigger be “queuejob,modifyjob,resvsub,movejob,runjob”, psycopg2 library is imported with no problem. But when I try the event trigger “execjob\_begin,execjob\_prologue,execjob\_epilogue,execjob\_preterm,execjob\_end,exechost\_periodic,execjob\_launch,exechost\_startup,execjob\_attach” pbs\_python can not import the library.

The code which I’ve tried:  
try:  
import psycopg2  
except:  
file.write("###########Can not import the module #################\n")

How can I solve this problem.  
Thank for any help.

---

<div class="post-metadata">

**Author:** ![adarsh](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@adarsh](https://community.openpbs.org/u/adarsh)\
**Post date:** [May 30, 2018, 2:10pm UTC](https://community.openpbs.org/t/psycopg2-on-pbs-hook/1012/2 "2018-05-30T14:10:52Z")

</div>

Please make sure you can import psycopg2 on your system python both on server and compute nodes

In my environment:

```
[root@pbs ~]# ls /usr/lib64/python2.7/site-packages/psycopg2
errorcodes.py extras.py _json.py psycopg1.py _range.pyo
errorcodes.pyc extras.pyc _json.pyc psycopg1.pyc tz.py
errorcodes.pyo extras.pyo _json.pyo psycopg1.pyo tz.pyc
extensions.py __init__.py pool.py _psycopg.so tz.pyo
extensions.pyc __init__.pyc pool.pyc _range.py
extensions.pyo __init__.pyo pool.pyo _range.pyc

```

Modify and add the below code to your hook:

```
import os,sys
lib_path_python27='/usr/lib64/python2.7/site-packages'
if lib_path_python27 not in sys.path:
    sys.path.append(lib_path_python27)
    print sys.path
    import psycopg2
    dir(psycopg2)

```

To test this, just run the code using pbs\_python abovecode.py , if this works, then  
it should work in your hook as well.

Thank you
