# Postgres write timestamp error

**URL:** https://discourse.nodered.org/t/postgres-write-timestamp-error/72643
**Category:** General
**Tags:** database, function-node
**Created:** [23 December 2022 17:14 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643 "2022-12-23T17:14:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kgakere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgakere/32/46080_2.png) [@kgakere](https://discourse.nodered.org/u/kgakere)
#### Post date: [23 December 2022 17:14 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/1 "2022-12-23T17:14:24Z")

</div>

Hi, i have a postgesdb that i am trying to inject a timestamp into a column.  
I have used a function node to convert the time to an appropriate format but still have errors.  
Kindly assist;

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/7909dd11f94d85df6f5ce1fd52899b435906d325.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81b47077eba2e7b6ea3498763ec455192843738d.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31646fa3212438bbec9701e19650d66779bf4f83.png)  
error: syntax error at or near "20"  
2022-12-23 20:10:57.716

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [23 December 2022 22:08 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/2 "2022-12-23T22:08:57Z")

</div>

Try removing the SSS from the format. I do not use postgres but looking at the allowed date formats none of them include anything other than YYY-MM-DD HH:mm:ss.

You could also try epoch if that is available to you as that seem as though it may be valid

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [23 December 2022 22:40 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/3 "2022-12-23T22:40:57Z")

</div>

You can not `$now().format()` You can `$moment().format()`  
But maybe we are not seeing all the picture.

---

<div class="post-metadata">

### Author: ![kgakere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgakere/32/46080_2.png) [@kgakere](https://discourse.nodered.org/u/kgakere)
#### Post date: [24 December 2022 05:38 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/4 "2022-12-24T05:38:06Z")

</div>

Tried removing the millisec but still the error is persisting

---

<div class="post-metadata">

### Author: ![kgakere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgakere/32/46080_2.png) [@kgakere](https://discourse.nodered.org/u/kgakere)
#### Post date: [24 December 2022 05:44 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/5 "2022-12-24T05:44:01Z")

</div>

```auto
[
    {
        "id": "5a2b67f8e1276282",
        "type": "tab",
        "label": "Flow 8",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "20f3f0eca8369002",
        "type": "postgresql",
        "z": "5a2b67f8e1276282",
        "name": "",
        "query": "INSERT INTO water_level_tank1(time) VALUES ({{msg.payload}}); \n",
        "postgreSQLConfig": "b91b213bbbb40562",
        "split": false,
        "rowsPerMsg": 1,
        "outputs": 1,
        "x": 730,
        "y": 220,
        "wires": [
            [
                "3f6eb8fe286e1404"
            ]
        ]
    },
    {
        "id": "3f6eb8fe286e1404",
        "type": "debug",
        "z": "5a2b67f8e1276282",
        "name": "debug 11",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 900,
        "y": 220,
        "wires": []
    },
    {
        "id": "c6af5f21fd73e3fe",
        "type": "inject",
        "z": "5a2b67f8e1276282",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 290,
        "y": 180,
        "wires": [
            [
                "6c3a8e033f6fb0f4"
            ]
        ]
    },
    {
        "id": "a5bef75bc183fb2b",
        "type": "debug",
        "z": "5a2b67f8e1276282",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 910,
        "y": 140,
        "wires": []
    },
    {
        "id": "6c3a8e033f6fb0f4",
        "type": "change",
        "z": "5a2b67f8e1276282",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$moment().format('YYYY-MM-DD HH:mm:ss')",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 500,
        "y": 180,
        "wires": [
            [
                "a5bef75bc183fb2b",
                "20f3f0eca8369002"
            ]
        ]
    },
    {
        "id": "b91b213bbbb40562",
        "type": "postgreSQLConfig",
        "name": "",
        "host": "127.0.0.1",
        "hostFieldType": "str",
        "port": "5432",
        "portFieldType": "num",
        "database": "postgres",
        "databaseFieldType": "str",
        "ssl": "false",
        "sslFieldType": "bool",
        "applicationName": "",
        "applicationNameType": "str",
        "max": "10",
        "maxFieldType": "num",
        "idle": "1000",
        "idleFieldType": "num",
        "connectionTimeout": "10000",
        "connectionTimeoutFieldType": "num",
        "user": "postgres",
        "userFieldType": "str",
        "password": "abcd1234",
        "passwordFieldType": "str"
    }
]

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [24 December 2022 07:03 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/6 "2022-12-24T07:03:20Z")

</div>

> [@kgakere](#):
>
> `INSERT INTO water_level_tank1(time) VALUES ({{msg.payload}})`

Could you try instead (timestamp should be inserted as a string):

```auto
INSERT INTO water_level_tank1(time) VALUES ('{{{ msg.payload }}}')

```

note i don't use postgres, but this is what [documentation](https://flows.nodered.org/node/node-red-contrib-postgresql) states.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 December 2022 09:09 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/7 "2022-12-24T09:09:16Z")

</div>

Are you trying to add the current time? If so then I would be surprised if Postgres does not have an option to automatically set a column to the current time when the record is added.

---

<div class="post-metadata">

### Author: ![kgakere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgakere/32/46080_2.png) [@kgakere](https://discourse.nodered.org/u/kgakere)
#### Post date: [25 December 2022 08:01 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/8 "2022-12-25T08:01:25Z")

</div>

Yes there is a default way to insert timestamp.  
Thanks for pointing me in the right direction.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4b2090ecf37aec5b039f8f4d58605634f49bc5a.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [8 January 2023 08:02 UTC](https://discourse.nodered.org/t/postgres-write-timestamp-error/72643/9 "2023-01-08T08:02:17Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
