Skip to content

Commit 775d63a

Browse files
committed
add postgres support for pandas.io.sql.get_schema
1 parent 4bbab1a commit 775d63a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

pandas/io/sql.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -360,28 +360,34 @@ def table_exists(name, con, flavor):
360360

361361
def get_sqltype(pytype, flavor):
362362
sqltype = {'mysql': 'VARCHAR (63)',
363-
'sqlite': 'TEXT'}
363+
'sqlite': 'TEXT',
364+
'postgres': 'text'}
364365

365366
if issubclass(pytype, np.floating):
366367
sqltype['mysql'] = 'FLOAT'
367368
sqltype['sqlite'] = 'REAL'
369+
sqltype['postgres'] = 'real'
368370

369371
if issubclass(pytype, np.integer):
370372
#TODO: Refine integer size.
371373
sqltype['mysql'] = 'BIGINT'
372374
sqltype['sqlite'] = 'INTEGER'
375+
sqltype['postgres'] = 'integer'
373376

374377
if issubclass(pytype, np.datetime64) or pytype is datetime:
375378
# Caution: np.datetime64 is also a subclass of np.number.
376379
sqltype['mysql'] = 'DATETIME'
377380
sqltype['sqlite'] = 'TIMESTAMP'
381+
sqltype['postgres'] = 'timestamp'
378382

379383
if pytype is datetime.date:
380384
sqltype['mysql'] = 'DATE'
381385
sqltype['sqlite'] = 'TIMESTAMP'
386+
sqltype['postgres'] = 'date'
382387

383388
if issubclass(pytype, np.bool_):
384389
sqltype['sqlite'] = 'INTEGER'
390+
sqltype['postgres'] = 'boolean'
385391

386392
return sqltype[flavor]
387393

@@ -393,9 +399,13 @@ def get_schema(frame, name, flavor, keys=None):
393399
column_types = zip(safe_columns, map(lookup_type, frame.dtypes))
394400
if flavor == 'sqlite':
395401
columns = ',\n '.join('[%s] %s' % x for x in column_types)
402+
elif flavor == 'mysql':
403+
columns = ',\n '.join('`%s` %s' % x for x in column_types)
404+
elif flavor == 'postgres':
405+
columns = ',\n '.join('%s %s' % x for x in column_types)
396406
else:
397-
columns = ',\n '.join('`%s` %s' % x for x in column_types)
398-
407+
raise ValueError("Don't have a template for that database flavor.")
408+
399409
keystr = ''
400410
if keys is not None:
401411
if isinstance(keys, basestring):

0 commit comments

Comments
 (0)