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

  • Home
  • SEARCH
  • 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 8908193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:07:04+00:00 2026-06-15T03:07:04+00:00

I had been using MySQL as database and had planned to move to postgresql.

  • 0

I had been using MySQL as database and had planned to move to postgresql. I had used aes_encrypt and aes_decrypt functions in MySQL extensively throughout my application. So whenever the encryption/decrytion fails, MySQL automatically returns ‘null’.

I am unsure how to handle the same in postgresql. Tried using the pgp_sym_encrypt/pgp_sym_decrypt functions. If the encryption key is wrong, it throws error “Wrong key/corrupt data”. I tried searching for some functions that could capture this error and return ‘null’ as in MySQL so that I need not modify my code. I had been searching but could not find one.

Has anybody used any error handling mechanism for individual queries? I had found that error handling can be done for procedures. But, I had to completely rewrite the entire application for that.

If you could share some details, it would be of great help. Thanks.

  • 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-06-15T03:07:05+00:00Added an answer on June 15, 2026 at 3:07 am

    If you wish to avoid modifying your code and have the functions return NULL on error, you can do this by wrapping them in a PL/PgSQL function that uses a BEGIN ... EXCEPTION block to trap the error.

    To do this, first I get the SQLSTATE for the error:

    regress=# \set VERBOSITY verbose
    regress=# SELECT pgp_sym_decrypt('fred','key');
    ERROR:  39000: Wrong key or corrupt data
    LOCATION:  decrypt_internal, pgp-pgsql.c:607
    

    I could use this directly in the error handler, but I prefer to use a symbolic name, so I look up the error name associated with 39000 in Appendix A – Error codes, finding that it’s the generic function call error external_routine_invocation_exception. Not as specific as we would’ve liked, but it’ll do.

    Now a wrapper function is required. Something like this must be defined, with one function for each overloaded signature of pgp_sym_decrypt that you wish to support. For the (bytea,text) form that returns text, for example:

    CREATE OR REPLACE FUNCTION pgp_sym_decrypt_null_on_err(data bytea, psw text) RETURNS text AS $$
    BEGIN
      RETURN pgp_sym_decrypt(data, psw);
    EXCEPTION
      WHEN external_routine_invocation_exception THEN
        RAISE DEBUG USING
           MESSAGE = format('Decryption failed: SQLSTATE %s, Msg: %s',
                            SQLSTATE,SQLERRM),
           HINT = 'pgp_sym_encrypt(...) failed; check your key',
           ERRCODE = 'external_routine_invocation_exception';
        RETURN NULL;
    END;
    $$ LANGUAGE plpgsql;
    

    I’ve chosen to preseve the original error in a DEBUG level message. Here’s a comparison of the original and wrapper, with full message verbosity and debug level output.

    Enable debug output to show the RAISE. Note that it also shows the *original query text of the pgp_decrypt_sym call, including parameters.

    regress=# SET client_min_messages = DEBUG;
    

    New wrapped function still reports the error if detailed logging is enabled, but returns NULL:

    regress=# SELECT pgp_sym_decrypt_null_on_err('redsdfsfdsfd','bobsdf');
    LOG:  00000: statement: SELECT pgp_sym_decrypt_null_on_err('redsdfsfdsfd','bobsdf');
    LOCATION:  exec_simple_query, postgres.c:860
    DEBUG:  39000: Decryption failed: SQLSTATE 39000, Msg: Wrong key or corrupt data
    HINT:  pgp_sym_encrypt(...) failed; check your key
    LOCATION:  exec_stmt_raise, pl_exec.c:2806
     pgp_sym_decrypt_null_on_err
    -----------------------------
    
    (1 row)
    

    compared to the original, which fails:

    regress=# SELECT pgp_sym_decrypt('redsdfsfdsfd','bobsdf');
    LOG:  00000: statement: SELECT pgp_sym_decrypt('redsdfsfdsfd','bobsdf');
    LOCATION:  exec_simple_query, postgres.c:860
    ERROR:  39000: Wrong key or corrupt data
    LOCATION:  decrypt_internal, pgp-pgsql.c:607
    

    Note that both forms show the parameters the function was called with when it failed. The parameters won’t be shown if you’ve used bind parameters (“prepared statements”), but you should still consider your logs to be security critical if you’re using in-database encryption.

    Personally, I think it’s better to do crypto in the app, so the DB never has access to the keys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to get quaqua out of our application. We had been using
I've been using the following code to move views around, and never had a
I have three tables in a MySQL database used in a music library application:
I'm just discovering PHPs sanitize and Validate filters, and I had been using MySQL's
I had been using SQL Server and am now using MySQL for a project.
Prior to the asp.net 4 RC update, I had been using the WebApi for
String string1 = abCdefGhijklMnopQrstuvwYz; String string2 = ABC; I had been using string1.startsWith(string2), which
We have a ASP.NET website project. In the past, we had been using asmx
follow up: while I had already been using swfobject with static publishing, dynamic publishing
I had been racking my brains over creating a vertical alignment in css using

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.