skip to Main Content

Noob question. Despite following the official PostgreSQL syntax I cannot insert multiple rows into a table. It always ends up adding all values to the first row.

I first created a table like this:

create table "DBSTest".Test (column1 varchar, column2 varchar, column3 varchar);

My update statement looks like this:

insert into "DBSTest".Test (column1, column2, column3) 
values (
('test1a','test1b'),
('test2a','test2b'),
('test3a','test3b')
);

I would expect the table to look like this:

column1 column2 column3
test1a test2a test3a
test1b test2b test3b

Instead it ends up like this:

column1 column2 column3
(test1a,test1b) (test2a,test2b) (test3a,test3b)

Apparently I’m getting something super wrong. I would appreciate any help on this, thanks 🙂

PS. Edited to get the table format right.

3

Answers


  1. In the VALUES clause of INSERT, each row of data (in your sample each line of data) goes into its own row of the table, not its own column.

    Login or Signup to reply.
  2. You are trying to insert 3 rows as (‘test1a’,’test1b’) in the first column

    Try this :

    insert into Test (column1, column2, column3) 
    values 
    ('test1a','test2a', 'test3b'),
    ('test1b','test2b', 'test3b');
    
    Login or Signup to reply.
  3. SQL inserts values by order of rows, not columns, i.e. your query should look like this:

    insert into dbstest (column1, column2, column3) values ('test1a', 'test2a', 'test3a'), ('test1b', 'test2b', 'test3b');

    This then returns the output as you specified:

    table

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search