Search code examples
pythonfacebookpostgresqltornado

tornado and postgresql


I'm using demo from facebook's tornado

But I don't want to use MySQL and trying to replace it with PG So I went ahead and modified like this :

define("port", default=8888, help="run on the given port", type=int)
define("pgsql_host", default="127.0.0.1:5432", help="blog database host")
define("pgsql_database", default="pgdb", help="blog database name")
define("pgsql_user", default="admin", help="blog database user")
define("pgsql_password", default="pgpass", help="blog database password")

and

        # Have one global connection to the blog DB across all handlers
    self.db = tornado.database.Connection(
        host=options.pgsql_host, database=options.pgsql_database,
        user=options.pgsql_user, password=options.pgsql_password)

But app just hangs when I run it, how to properly migrate it to PG?

Actually I get error after some time:

    Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/database.py", line 84, in __init__
    self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/database.py", line 101, in reconnect
    self._db = MySQLdb.connect(**self._db_args)
  File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")

Why is it connecting to MySQL?


Solution

  • Are you sure you can use it with different RDBMS including PostgreSQL? It uses torndb package which is written to work with MySQL.

    So i think that if you want to use it with PostgreSQL you should write your own torndb package for postgresql.

    EDIT: As you can see at Tornado Wiki you have to have postgresql wrapper (momoko or psycopg) there are some links for further study.

    EDIT2: Momoko description:
    "An asynchronous Psycopg2 wrapper for Tornado."