Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:49:01+00:00 2026-05-27T13:49:01+00:00

psycopg2’s doc has an instruction on how to cast a specific type to a

  • 0

psycopg2’s doc has an instruction on how to cast a specific type to a different type:

Psycopg converts decimal/numeric database types into Python Decimal objects. Can I have float instead?
You can register a customized adapter for PostgreSQL decimal type:

    DEC2FLOAT = psycopg2.extensions.new_type(
        psycopg2.extensions.DECIMAL.values,
        'DEC2FLOAT',
        lambda value, curs: float(value) if value is not None else None)
    psycopg2.extensions.register_type(DEC2FLOAT)

But my data will have a mixture of str, float, integer, decimal, date type, etc. How do i tell psycopg2 to cast any non string value to string? I don’t want to register a customized adapter everytime I encounter a new type because my query could change.
I hope I make the situation clear enough.
thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:49:02+00:00Added an answer on May 27, 2026 at 1:49 pm

    You could delete all the typecasters and receive everything in the postgres string representation:

    import psycopg2
    
    for k in psycopg2.extensions.string_types.keys():
        del psycopg2.extensions.string_types[k]
    
    cnn = psycopg2.connect('')
    cur = cnn.cursor()
    cur.execute("select 1::int, now()::timestamp, 'hello'::text")
    cur.fetchone()
    ('1', '2011-12-19 16:50:11.396855', 'hello')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use psycopg2 for accessing my postgres database in python. My function should create
Background : Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2
I am able to connect to my remote database using psycopg2 in standalone Python
I updated a database table using postgresql from python My code was import psycopg2
I'm using psycopg2 to access a postgresql 8.4 database, and on an update query
psycopg2 converts NULL to None through the NoneAdapter (right?) How do I create another
I am working on PostgreSQL and psycopg2. Trying to get feed data which is
I have Pythong2.6, psycopg2 and pgAdmin3 installed using Macports. My settings.py is: DATABASE_ENGINE =
I'm getting an error whilst trying to install psycopg2 on ubuntu 9.10 64 bit.
Is there a way to make psycopg and postgres deal with errors without having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.