--- sql_orig.py	2010-09-18 07:07:22.000000000 -0500
+++ sql_patch.py	2010-09-27 10:57:32.843453900 -0500
@@ -980,7 +980,7 @@
         elif not is_jdbc and self._uri.startswith('postgres://'):
             self._dbname = 'postgres'
             m = \
-                re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[^@]*))?@(?P<host>[^\:/]+)(\:(?P<port>[0-9]+))?/(?P<db>.+)$'
+                re.compile('^(?P<user>[^:@]+)(\:(?P<passwd>[^@]*))?@(?P<host>[^\:@/]+)(\:(?P<port>[0-9]+))?/(?P<db>[^\?]+)(\?sslmode=(?P<sslmode>.+))?$'
                            ).match(self._uri[11:])
             if not m:
                 raise SyntaxError, "Invalid URI string in SQLDB"
@@ -998,9 +998,12 @@
                 raise SyntaxError, 'Database name required'
             port = m.group('port') or '5432'
 
-            msg = \
-                "dbname='%s' user='%s' host='%s' port=%s password='%s'"\
-                 % (db, user, host, port, passwd)
+            sslmode = m.group('sslmode') or 'prefer'
+
+            msg = ("dbname='%s' user='%s' host='%s'"
+                   "port=%s password='%s' sslmode='%s'") \
+                 % (db, user, host, port, passwd, sslmode)
+
             self._pool_connection(lambda : psycopg2.connect(msg))
             self._connection.set_client_encoding('UTF8')
             self._cursor = self._connection.cursor()
