Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Loading a column of data in postgres?

    Hello,

    How do I load a column of data in postgres? For example, if I have a table and I add a new column via alter table that is an integer, (Say, if the current table has 1000 rows of data) and I want to add 1000 values to the new column. What's the best approach to doing this? I've looked at the Copy command but it doesn't seem (at least from the documentation I've read) allow copies into single columns. i've tried doing an update like this:

    update table foo set column bar = (10,20,30,40,50) but that complains about setting an integer to type 'record', and requiring a cast.

    Do I need to write a postgres stored procedure to somehow iterate over each row and add the column from a data source?

    Thanks,

    -- redmage123

  2. #2
    Join Date
    Nov 2003
    Posts
    2,815
    How do you know which value goes into which row? Do you have a mapping of primary key values to the value for the new column?
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

  3. #3
    Join Date
    Dec 2012
    Posts
    2
    Well, it's not so much that there is a specific relationship between the primary key and the value, in this case the primary key is just a serial value.

    So, for example if the columns are:

    id, item_name (varchar) amount_in_stock (integer), and I want to add an integer column (minimum_required_amount) using alter table, how do i then load values for this column into the table? There isn't a relationship per se between the key and the new column. I tried doing an update my_table set minimum_required_amount = (10, 20,30,20,10);
    but postgres doesn't seem to like this. Basically, I just want to load this data in starting at the first row into the minimum_required_amount column.

    -- redmage123

  4. #4
    Join Date
    Nov 2003
    Posts
    2,815
    Quote Originally Posted by redmage123 View Post
    I tried doing an update my_table set minimum_required_amount = (10, 20,30,20,10);
    In the above example: which row should get the value 10, which row should get the value 20 and which row should get the value 30?

    Basically, I just want to load this data in starting at the first row into the minimum_required_amount column.
    Please show us some sample data before updating your table, and the expected result after the UPDATE has run.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •