Search code examples
pythonpostgresqlpandasodbcpypyodbc

Pandas attribute names with pypyodbc on centos return in Chinese


I am trying to connect using pypyodbc and pandas to a postgres DB. I am using Python 2.7 on a Centos 7 machine.

import pypyodbc
import pandas as pd

conn = pypyodbc.connect("Driver={PostgreSQL};Server=xxx;Port=xxx;Database=xxx;Uid=xxx;Pwd=xxx;")
cur = conn.cursor()
cur.execute("select * from client_list")
col_names = []
for elt in cur.description:
    col_names.append(elt[0])
myDF=pd.DataFrame(cur.fetchall(),columns=col_names)
conn.close()

My connection string points to a postgresql DB, for which I've configured an ODBC connection. I've configured both odbcinst.ini and odbc.ini:

/etc/odbc.ini -

[AlgoDB]
Description         = PostgreSQL connection to 'AlgoDB' database
Driver              = /usr/lib64/psqlodbc.so
Database            = xxx
Servername          = xxx
UserName            = xxx
Password            = xxx
Port                = 5432
Protocol            = 8.1
ReadOnly            = No
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No

/etc/odbcinst.ini:

[PostgreSQL]
Description     = ODBC for PostgreSQL
Driver          = /usr/lib/psqlodbcw.so
Setup           = /usr/lib/libodbcpsqlS.so
Driver64        = /usr/lib64/psqlodbc.so
Setup64         = /usr/lib64/libodbcpsqlS.so
FileUsage       = 1

I am able to retrieve the results of the query, however, the field names are returned in Chinese:

for index, row in myDF.iterrows():
    print(row)

Resulting in:

汣敩瑮湟浡e      my client's name
灡彰牵l浡e       https://my client's url/

When I work with a windows machine, I get the attribute names as supposed in English:

client_name      my client's name
client_url       https://my client's url/

If I use isql and run the query there, the field names are in English:

sudo isql -v AlgoDB xxx xxx

What am I doing wrong?? Thank you.


Solution

  • I gave up on pypyodbc after trying to reinstall the ODBC drivers and switched to sqlalchemy:

    import sqlalchemy as sa
    import pandas as pd
    eng = sa.create_engine("postgresql://user:pass@ip:5432/DBName")
    myDF = pd.read_sql("select * from client_list",eng)