Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql example #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/demo_postgresql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pandas import DataFrame
from pandasql import PandaSQL

# a dataframe
df1 = DataFrame({"a": [1, 2, 3, 1], "b": [7, 1, 5, 3]})

# establish postgresql connection using psycopg2 driver
# log in with username: tubh; password: tubh; using unix socket at /tmp
sqlClass = PandaSQL("postgresql+psycopg2://tubh:tubh@/tubh?host=/tmp")

# try function GREATEST which is not supported by sqlite by is supported by postgresql
result = sqlClass(("select GREATEST(tbl.a, tbl.b) as maxA_B from tbl"), {"tbl": df1})
print(result)
4 changes: 2 additions & 2 deletions pandasql/sqldf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from contextlib import contextmanager
from pandas.io.sql import to_sql, read_sql
from sqlalchemy import create_engine
from sqlalchemy import create_engine, text
import re
from warnings import catch_warnings, filterwarnings
from sqlalchemy.exc import DatabaseError, ResourceClosedError
Expand Down Expand Up @@ -89,7 +89,7 @@ def conn(self):

def _init_connection(self, conn):
if self.engine.name == 'postgresql':
conn.execute('set search_path to pg_temp')
conn.execute(text('set search_path to pg_temp'))

def _set_text_factory(self, dbapi_con, connection_record):
dbapi_con.text_factory = str
Expand Down