Python SDK with Postgres DB

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?

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…

.. 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

I found another way using the java sdk

I also found this option…

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

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

Configuration #1 does not work

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

Configuration #2 does not work

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

Configuration #3 does not work

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

Configuration #4 does not work

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

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

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()

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...

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.

It looks like psycopg is written in C under the hood, fwiw psycopg2/psycopg at master · psycopg/psycopg2 · GitHub

1 Like