If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Loading a column of data in postgres?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Nov 2003
Posts: 2,805
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
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: Nov 2003
Posts: 2,805
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?

Quote:
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On