# Import psycopg2 error when running python node

**URL:** <https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221>\
**Category:** General\
**Created:** [16 April 2019 09:02 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221 "2019-04-16T09:02:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 09:02 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/1 "2019-04-16T09:02:31Z")

</div>

I am trying to execute python script which invokes postgress database connection on a remote machine and I see following error, though the psycopg2 package is installed as part of python packages. I have installed "node-red-contrib-python3-function" , "node-red-contrib-python-function" and "node-red-contrib-pythonshell"  
.  
msg.payload : string[136]  
string[136]  
Traceback (most recent call last):  
File "dbconnect.py", line 5, in   
import psycopg2  
ImportError: No module named psycopg2

I see this error if I use either of "python node" or pythonshell or "exec" node.  
Any pointers in this regard to unblock me.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 April 2019 09:06 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/2 "2019-04-16T09:06:58Z")

</div>

And it works from the commandline ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 April 2019 09:11 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/3 "2019-04-16T09:11:48Z")

</div>

Why call a python script? Why not query the database from node red?

There are about 6 nodes for postgres ([search postgres here](https://flows.nodered.org/))

e.g. [This one](https://flows.nodered.org/node/node-red-contrib-postgres-variable)

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 09:33 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/4 "2019-04-16T09:33:58Z")

</div>

Yes, it works from command line

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 April 2019 09:34 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/5 "2019-04-16T09:34:51Z")

</div>

is Node-RED running in the same environment as your command line user ? Ie are the python packages available globally - or just to your user. ?

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 09:49 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/6 "2019-04-16T09:49:44Z")

</div>

Yes, its running on same environment as command line user.  
Infact I have my python script placed at this location.  
/var/lib/docker/overlay2/18abcc8e24afc641fa13752e9d5477f79d5fe2d2d9a643496b1473995599fd9b/merged/usr/src/node-red  
I execute the python script as "root" user, which is working fine from command line.  
From node red, I get the import error(\ ***"ImportError: No module named psycopg2**")not sure whats the missing link here.

Should I have this file in different path?  
The reason why I am using python script to connect to postgres DB as I need to pass certificate and key file to connect to my database.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 April 2019 09:52 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/7 "2019-04-16T09:52:37Z")

</div>

> [@prasad.dp5](#):
>
> The reason why I am using python script to connect to postgres DB as I need to pass certificate and key file to connect to my database.

As I don't use postgres nor the available nodes so I cant answer whether they support that i'm afraid.

However, it would be good for the community if they did. Perhaps give them a try & feedback a request/issue via github?

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 10:18 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/8 "2019-04-16T10:18:34Z")

</div>

Sure, I will do that.  
My current issue to run python script that connects to postgres is running into "psycopq2" import error, that I need to get it resolved to make me get going.

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 12:19 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/9 "2019-04-16T12:19:35Z")

</div>

I figured out the problem, the problem is the docker image of node-red do not contain "python-psycopg2" packages, you can verify it by getting into node-red shell.

1. docker exec -it mynodered /bin/bash  
I ran it manually, it failed again.

node-red@77f884ce09a9:~$ python dbconnect.py  
Traceback (most recent call last):  
File "dbconnect.py", line 5, in   
import psycopg2  
ImportError: No module named psycopg2

However the global python present in my system had this when I executed it as root.

How can I add this "python-psycopg2" package to node-red docker image.  
I tried even building it, it did not help.  
I did modify the Dockerfile to install "psycopg2 and tried to build the docker image of "node-red" it did not help.

_RUN apt-get install python-psycopg2_

Step 10/14 : RUN apt-get install python-psycopg2  
---\> Running in 506c3d9919f6  
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)  
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?  
The command '/bin/sh -c apt-get install python-psycopg2' returned a non-zero code: 100

Any help in this regard is appreciated.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 April 2019 12:44 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/10 "2019-04-16T12:44:12Z")

</div>

You need to be the root user to run apt-get. I _think_ the following will help.

(Please pick if you want to continue this discussion here or in slack... not going to split it over both places)

```auto
USER root
RUN apt-get install python-psycopg2
USER node-red

```

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [16 April 2019 13:05 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/11 "2019-04-16T13:05:36Z")

</div>

I tried as you suggested, look like its unable to pick the package.

Step 3/4 : RUN apt-get install python-psycopg2  
---\> Running in 7a41d497a588  
Reading package lists...  
Building dependency tree...  
Reading state information...  
E: Unable to locate package python-psycopg2  
The command '/bin/sh -c apt-get install python-psycopg2' returned a non-zero code: 100

Am I missing something, I tried to install "pip" even that had run into same error.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 April 2019 13:10 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/12 "2019-04-16T13:10:22Z")

</div>

I'm just making educated guesses as it isn't something I've ever done.

A quick google search finds lots of threads talking about installing psycopg2 into a Docker image. I suggest you go read those threads and follow their advice based on actual experience.

---

<div class="post-metadata">

**Author:** ![prasad.dp5](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@prasad.dp5](https://discourse.nodered.org/u/prasad.dp5)\
**Post date:** [7 May 2019 08:03 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/13 "2019-05-07T08:03:10Z")

</div>

Thanks, I forgot to update this thread. I got this issue resolved.  
I followed these steps.

1. Install node-red
2. login as root using node-red CLI
3. Add the required packages  
apt-get update  
apt-get install -y python-pip  
apt-get install -y python-psycopg2

Create a docker build with these packages in it and re-install node-red.

---

<div class="post-metadata">

**Author:** ![Premkumar02](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@Premkumar02](https://discourse.nodered.org/u/Premkumar02)\
**Post date:** [7 August 2020 13:33 UTC](https://discourse.nodered.org/t/import-psycopg2-error-when-running-python-node/10221/14 "2020-08-07T13:33:27Z")

</div>

how did u login as root?
