# Python SDK with Postgres DB

**URL:** <https://openziti.discourse.group/t/python-sdk-with-postgres-db/614>\
**Category:** SDK Questions\
**Created:** [June 24, 2022, 11:20pm UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614 "2022-06-24T23:20:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [June 24, 2022, 11:20pm UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/1 "2022-06-24T23:20:29Z")

</div>

I have the Java SDK Postgres example working.. and are now starting to try new things.. like use the Python SDK to connect to Postgres

One thing I noticed in the Java Postgres example, is that the JDBC driver does not need a port number.

I am curious to know how this works.. because you need a port number for all other end points.. is there something specific to this configuration?

The reason why I am exploring this.. because its the exact problem I have to with the Python SDK when attempting to connect to Postgres.

> you need a port number.

Maybe I am doing something wrong..

Any tips to point me in the right direction?

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [June 24, 2022, 11:27pm UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/2 "2022-06-24T23:27:14Z")

</div>

I think I may have found a way forward… as there is a sockets sample… that can be used with a sockets db connector.

```
sock = openziti.socket(type = SOCK_STREAM)
sock.connect(('httpbin.ziti', 2000))

```

I am going to give this a go…

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [June 24, 2022, 11:55pm UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/3 "2022-06-24T23:55:13Z")

</div>

> [@markamind](#):
>
> I am curious to know how this works… because you need a port number for all other end points… is there something specific to this configuration?

.. the answer is in how its configured

> this creates the addressable URL for a specific port  
> ziti edge create config private-postgres-intercept.v1 intercept.v1 '{"protocols":["tcp"],"addresses":["zitified-postgres"], "portRanges":[{"low":5432, "high":5432}]}'

> this makes links in the postgres database host  
> ziti edge create config private-postgres-host.v1 host.v1 '{"protocol":"tcp", "address":"postgres-db","port":5432 }'

> this connects the two together  
> ziti edge create service private-postgres --configs private-postgres-intercept.v1,private-postgres-host.v1 -a "private-postgres-services"

In relation to the port number, I now think I see the connection, its using the default ports associated with the jdbc driver.. which is 5432 for postgres

[https://jdbc.postgresql.org/documentation/head/connect.html](https://jdbc.postgresql.org/documentation/head/connect.html)

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [June 25, 2022, 3:17am UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/4 "2022-06-25T03:17:18Z")

</div>

I found another way using the java sdk

> <https://stackoverflow.com/questions/12052241/use-java-library-from-python-python-wrapper-for-java-library>

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [June 25, 2022, 4:47am UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/5 "2022-06-25T04:47:35Z")

</div>

I also found this option…

> **[Connect PostgreSQL using Python and Jdbc Driver- Example - DWgeek.com](https://dwgeek.com/connect-postgresql-using-python-and-jdbc-driver-example.html/)**
>
> Connect PostgreSQL using Python and Jdbc Driver, Syntax, Example, PostgreSQL JDBC Connection String, Jaydebeapi, PostgreSQL JDBC Driver

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [July 10, 2022, 10:39am UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/6 "2022-07-10T10:39:46Z")

</div>

Quick note.. checking if anyone can help out here... I am close to being very stuck.

What I would like to clarify is

> can you actually use a MonkeyPatch to connect to a Postgres DB?

I have explored how a MonkeyPatch could be used with the psycopg2 library, but I have not been able to get it work successfully.

For instance, this is what a standard connection would look like

```auto
psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="zitified-postgres",
                                  port="5432",
                                  database="simpledb")

```

**Configuration #1 does not work**

```auto
connection = openziti.monkeypatch()
psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="zitified-postgres",
                                  port="5432",
                                  database="simpledb")

```

**Configuration #2 does not work**

```auto
connection = openziti.monkeypatch(psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="zitified-postgres",
                                  port="5432",
                                  database="simpledb"))

```

**Configuration #3 does not work**

```auto
connection = openziti.monkeypatch(psycopg2).connect(user="postgres",
                                  password="postgres",
                                  host="zitified-postgres",
                                  port="5432",
                                  database="simpledb")

```

**Configuration #4 does not work**

```auto
with openziti.monkeypatch():
        connection = psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="zitified-postgres",
                                  port="5432",
                                  database="simpledb")

```

What I have worked out is that I am missing something.

> any tips on what I am doing wrong?

@TheLumberjack @ekoby @qrkourier

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [July 10, 2022, 10:54am UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/7 "2022-07-10T10:54:27Z")

</div>

Small break through… as I did manage to have this code run without generating an error… but I still need to make it connect to the database and run a SQL query

```auto
import openziti
from socket import SOCK_STREAM,SHUT_WR

if __name__ == ' __main__':
    print("Ziti SDK version = {0}".format(openziti.version()))
    sock = openziti.socket(type = SOCK_STREAM)
    sock.connect(('zitified-postgres', 5432))
    sock.shutdown(SHUT_WR)
    openziti.shutdown()

```

---

<div class="post-metadata">

**Author:** ![markamind](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/markamind/32/157_2.png) [@markamind](https://openziti.discourse.group/u/markamind)\
**Post date:** [July 10, 2022, 11:03pm UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/8 "2022-07-10T23:03:58Z")

</div>

After some sleep.. and my mind doing its magic.. this is how I now view the problem of wanting to use the Python SDK to make a connection to a Postgres DB.

> start in Gloang  
> then compile into C  
> make reference to the C library in Python

The inspiration of this came from the following article.. though I have no idea of how far I will get...

> **[Running Go code from Python](https://medium.com/analytics-vidhya/running-go-code-from-python-a65b3ae34a2d)**
>
> Python is a great language that can be picked up easily by a new developer and be productive in a short amount time, it is clean code by…

From what I understand, the MonkeyPatch does not work with Psycopg because of how the package is designed. Hence, the solution needs to be developed in a lower language such as C. You write C an integrate the C SDK, but I don't know C.. and have just started to use Gloang.

> let me know if I have something wrong.

---

<div class="post-metadata">

**Author:** ![smilindave26](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/smilindave26/32/185_2.png) [@smilindave26](https://openziti.discourse.group/u/smilindave26)\
**Post date:** [July 11, 2022, 12:37am UTC](https://openziti.discourse.group/t/python-sdk-with-postgres-db/614/9 "2022-07-11T00:37:11Z")

</div>

It looks like psycopg is written in C under the hood, fwiw [psycopg2/psycopg at master · psycopg/psycopg2 · GitHub](https://github.com/psycopg/psycopg2/tree/master/psycopg)
