Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert value into a column in PostgreSQL

I am trying to figure out how to insert the same value into the entire column of a table? The table already exists and I have an empty column into which I would like to insert a certain value, for example, today's date. I have only found sources explaining how to insert values into an entire row.

like image 881
nix Avatar asked Jul 08 '10 15:07

nix


People also ask

How do I add values to a column in PostgreSQL?

Syntax. INSERT INTO TABLE_NAME (column1, column2, column3,... columnN) VALUES (value1, value2, value3,... valueN);

How do I add a value to an existing column?

INSERT INTO Syntax 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How do I add an entry to PostgreSQL?

First thing to do is specify the table name followed by the columns where you want to insert the data or rows. Secondly, you must list a comma-separated value list after the VALUES clause. The value list must be in the same order as the columns list specified after the table name.

How do you add values in pgAdmin?

To insert character data, you enclose it in single quotes (') for example 'PostgreSQL Tutorial' . If you omit required columns in the INSERT statement, PostgreSQL will issue an error. In case you omit an optional column, PostgreSQL will use the column default value for insert.


2 Answers

UPDATE myTable SET myColumn='newValue'

newValue can also be an expression.

see http://www.postgresql.org/docs/current/static/sql-update.html

like image 86
dweeves Avatar answered Nov 15 '22 04:11

dweeves


I think we need a bit more info to understand the issue, it sounds like you just want...

INSERT INTO table_foo (my_empty_column_name) values (current_date);

If you've already got data there and you want to UPDATE that column for all rows then...

UPDATE table_foo SET my_empty_column_name = current_date;
like image 37
rfusca Avatar answered Nov 15 '22 04:11

rfusca