and I would like to return the correct Postgresql data type so as to create the correct table for it:
datatype = type(num)
There is no such thing in pg. Type mapping is often ambiguous, so that pg does not mandate a particular mapping. You could use some higher level library like http://datamapper.org/ for this job, but most people prefer to explicitly define their SQL column types in one or another way.