0

Working with Postgres 14 on Win 10. Running this simple query:

mydata=# SELECT * FROM contact_test;
 cntct_id | fname | lname | email | street | unit | town | state | post_code
----------+-------+-------+-------+--------+------+------+-------+-----------
(0 rows)


mydata=# INSERT INTO contact_test (fname, lname, email) VALUES ('Robert', 'Johnson', 'rjohnsonn@gmail.com');
ERROR:  malformed array literal: "Robert"
LINE 1: ...T INTO contact_test (fname, lname, email) VALUES ('Robert', ...
                                                             ^
DETAIL:  Array value must start with "{" or dimension information.

I looked up the syntax here:

The sample essentially looks the same as my statement:

INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99);

What am I missing?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
tom_ep
  • 1
  • 2
  • 1
    What is the type of the `fname` column? If you are using psql, you can check with `\d contact_test`. – Ture Pålsson May 05 '23 at 04:16
  • Here is the output: ~~~ cntct_id | bigint | | not null | generated always as identity fname | "char"[] | | not null | lname | "char"[] | | not null | email | "char" | | not null | – tom_ep May 05 '23 at 04:21
  • I must confess that I’m not sure how to interpret `"char"[]`, but I *think* it means that the column is of type “array of single characters”. Is that what you want? If you want to handle multiple first names, something like `text[]` would seem more appropriate or, for a single first name, just `text`. – Ture Pålsson May 05 '23 at 04:50
  • @TurePålsson is right, you should consult [the docs](https://www.postgresql.org/docs/current/arrays.html). it says, insertion on array should use `{` - something like `{"name1", "name2"}` which does not suit the simple need to store variable-length string. we have no clue why did you make the columns this way. – Bagus Tesa May 05 '23 at 05:22
  • Please [edit] your question to add the all-essential table definition. – Erwin Brandstetter May 05 '23 at 05:33

1 Answers1

4

Given the table definition you added in a comment, this statement would work:

INSERT INTO contact_test (fname, lname, email) VALUES ('{R}', '{J}', 'r');

fname and lname are array types, so you need to pass array literals (or array constructors). Examples:

But your table definition is nonsense, of course. You don't want the internal enumeration type "char", which holds a single ASCII letter. You want varchar or text. And I am not convinced you want array types, either. ("char"[] is an array of "char".)
Fix your table definition accordingly! Like:

CREATE TABLE contact_test (
  cntct_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY
, fname text NOT NULL
, lname text NOT NULL
, email text NOT NULL
);

Then your original INSERT statement works.

See:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Interesting. I made this table using pgAdmin4, a GUI to postgresql. The column creation dialog didn't offer "varchar" or "text" as a type option. I'll recreate the table in psql as you recommended and try it out. Thanks much! – tom_ep May 05 '23 at 14:27
  • @tom_ep pgAdmin4 has those choices, though `varchar` is shown as `character varying` or `character varying []` for array. – Adrian Klaver May 05 '23 at 16:00
  • @Adrian Klaver I wish I could post an image here, (can I? New to stackoverflow), The options I see in the Add Column dialog are: "char", "char"[], aclitem, aclitem[], bigint, bigint[], bigserial, bit. That's it! – tom_ep May 05 '23 at 17:36
  • 1
    @tom_ep, pgAdmin4 version? The list you show is sorted and I'm betting you are only looking at the top selections. I'm also betting the drop down list under Data Type has a vertical slide bar that allows you to get past the top items and into more choices. If not there is something wrong with your pgAdmin4 install. – Adrian Klaver May 05 '23 at 17:44
  • @Adam Klaver Wow, do I feel like an idiot! In my defense, it is a slide dot almost the same color as the background.Time for new glasses! Yes, there are many more type options. Thanks much for your patience! – tom_ep May 05 '23 at 18:23