Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting date into PostgreSQL: Columns from and to [duplicate]

I can't insert a date into a PostgreSQL 9.6 table.

This is the INSERT Query:

INSERT INTO rates (idproperty, from, to, price)
VALUES (1, '2017-03-14', '2017-04-30', 130);

And that's the result:

ERROR:  syntax error at or near "from"
LINE 1: INSERT INTO rates (idproperty, from, to, price)

The columns from and to are defined with the data type date.

Thanks

like image 738
Aleix Alcover Avatar asked Mar 13 '17 15:03

Aleix Alcover


2 Answers

I believe you have used postgresql reserved words - from and to to create your table. In order to use them in your query, they need to be enclosed in quotes "

Try it this way:

INSERT INTO rates (idproperty, "from", "to", price)
VALUES (1, '2017-03-14', '2017-04-30', 130);
like image 107
user3402754 Avatar answered Oct 12 '22 02:10

user3402754


from and to are reserved words in PostgreSQL. You need to escape them with ":

-- This fails:

test=# SELECT from FROM rates;
ERROR:  syntax error at or near "FROM"
LINE 1: SELECT from FROM rates;
                    ^

-- This works:

test=# SELECT "from" FROM rates;
 from 
------
(0 rows)

See list of reserved words: https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html

like image 27
Gab Avatar answered Oct 12 '22 01:10

Gab