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 8810191
In Process

The Archive Base Latest Questions

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

I’ve been evaluating PostgreSQL’s hstore functionality (9.2) and the only thing the fine manual

  • 0

I’ve been evaluating PostgreSQL’s hstore functionality (9.2) and the only thing the fine manual isn’t explicit about is how to rename keys. For instance, how could I rename the key c to ai_count?

"c"=>"3", "ai_voltage"=>"3", "ai_temperature"=>"28"

I’m thinking there is no direct way to do this and that it involves duplicating the c key to a ai_count key, then dropping the c key. How can I do this, ideally as a one-liner which can be applied to multiple records?

  • 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-14T03:04:10+00:00Added an answer on June 14, 2026 at 3:04 am

    I think you’re right that you have to pull the old pair out and put the new pair (with the renamed key) back in.

    You could do it with a one-liner:

    (h - from_key) || hstore(to_key, h -> from_key)
    

    where h is the hstore, from_key is the key you want to change and to_key is what you want to change it to. That will return a new hstore with the desired change but it assumes that from_key is in h; if from_key isn’t in h then you’ll end up with a to_key -> NULL in your hstore. If you, like all sane people, don’t want the stray NULL then I’d wrap the logic in a simple function to make it easier to add an existence check; something like this:

    create or replace function
    change_hstore_key(h hstore, from_key text, to_key text) returns hstore as $$
    begin
        if h ? from_key then
            return (h - from_key) || hstore(to_key, h -> from_key);
        end if;
        return h;
    end
    $$ language plpgsql;
    

    Then you can say both of these and get the expected results:

    => select change_hstore_key('a=>1,b=>2,c=>3'::hstore, 'b', 'pancakes');
          change_hstore_key       
    ------------------------------
     "pancakes"=>"2", "a"=>"1", "c"=>"3"
    
    => select change_hstore_key('a=>1,b=>2,c=>3'::hstore, 'pancakes', 'X');
          change_hstore_key       
    ------------------------------
     "a"=>"1", "b"=>"2", "c"=>"3"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.