How to insert timestamp to postgreSQL database
I have a problem inserting timestamp to my psql database using Go.
I form my timestamp with this line:
datetime := currentTime.Format("02-01-2006 15:04:05")
My sql query is:
SqlStatement := `
INSERT INTO readings (date, temp, humi)
VALUES ($1, $2, $3)`
And then my call to the psql DB is:
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
(As you can see I have some other variables here but they are not causing any problems.)
When I execute my code I get this error:
pq: date/time field value out of range: "21-11-2018 22:19:59"
Which as I understand it means that the format is not correct.
YET when I input exact same query directly to psql console it successfully adds record(line) to the table.
INSERT INTO readings (date, temp, humi) VALUES ('02-01-2006 15:04:05', 20, 30);
Side note: This code worked ok before I changed column type from character(20) to timestamp, I even tried incorporating CAST in the SQL but I got the same error.
sql postgresql go timestamp
add a comment |
I have a problem inserting timestamp to my psql database using Go.
I form my timestamp with this line:
datetime := currentTime.Format("02-01-2006 15:04:05")
My sql query is:
SqlStatement := `
INSERT INTO readings (date, temp, humi)
VALUES ($1, $2, $3)`
And then my call to the psql DB is:
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
(As you can see I have some other variables here but they are not causing any problems.)
When I execute my code I get this error:
pq: date/time field value out of range: "21-11-2018 22:19:59"
Which as I understand it means that the format is not correct.
YET when I input exact same query directly to psql console it successfully adds record(line) to the table.
INSERT INTO readings (date, temp, humi) VALUES ('02-01-2006 15:04:05', 20, 30);
Side note: This code worked ok before I changed column type from character(20) to timestamp, I even tried incorporating CAST in the SQL but I got the same error.
sql postgresql go timestamp
1
It's the21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.
– mkopriva
Nov 21 '18 at 23:07
... alternatively you can change the datestyle setting withSET datestyle TO dmy;
(postgresql.org/docs/9.4/…)
– mkopriva
Nov 21 '18 at 23:14
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13
add a comment |
I have a problem inserting timestamp to my psql database using Go.
I form my timestamp with this line:
datetime := currentTime.Format("02-01-2006 15:04:05")
My sql query is:
SqlStatement := `
INSERT INTO readings (date, temp, humi)
VALUES ($1, $2, $3)`
And then my call to the psql DB is:
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
(As you can see I have some other variables here but they are not causing any problems.)
When I execute my code I get this error:
pq: date/time field value out of range: "21-11-2018 22:19:59"
Which as I understand it means that the format is not correct.
YET when I input exact same query directly to psql console it successfully adds record(line) to the table.
INSERT INTO readings (date, temp, humi) VALUES ('02-01-2006 15:04:05', 20, 30);
Side note: This code worked ok before I changed column type from character(20) to timestamp, I even tried incorporating CAST in the SQL but I got the same error.
sql postgresql go timestamp
I have a problem inserting timestamp to my psql database using Go.
I form my timestamp with this line:
datetime := currentTime.Format("02-01-2006 15:04:05")
My sql query is:
SqlStatement := `
INSERT INTO readings (date, temp, humi)
VALUES ($1, $2, $3)`
And then my call to the psql DB is:
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
(As you can see I have some other variables here but they are not causing any problems.)
When I execute my code I get this error:
pq: date/time field value out of range: "21-11-2018 22:19:59"
Which as I understand it means that the format is not correct.
YET when I input exact same query directly to psql console it successfully adds record(line) to the table.
INSERT INTO readings (date, temp, humi) VALUES ('02-01-2006 15:04:05', 20, 30);
Side note: This code worked ok before I changed column type from character(20) to timestamp, I even tried incorporating CAST in the SQL but I got the same error.
sql postgresql go timestamp
sql postgresql go timestamp
edited Nov 22 '18 at 7:48
Flimzy
37.7k96497
37.7k96497
asked Nov 21 '18 at 22:30
RobertRobert
104
104
1
It's the21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.
– mkopriva
Nov 21 '18 at 23:07
... alternatively you can change the datestyle setting withSET datestyle TO dmy;
(postgresql.org/docs/9.4/…)
– mkopriva
Nov 21 '18 at 23:14
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13
add a comment |
1
It's the21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.
– mkopriva
Nov 21 '18 at 23:07
... alternatively you can change the datestyle setting withSET datestyle TO dmy;
(postgresql.org/docs/9.4/…)
– mkopriva
Nov 21 '18 at 23:14
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13
1
1
It's the
21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.– mkopriva
Nov 21 '18 at 23:07
It's the
21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.– mkopriva
Nov 21 '18 at 23:07
... alternatively you can change the datestyle setting with
SET datestyle TO dmy;
(postgresql.org/docs/9.4/…)– mkopriva
Nov 21 '18 at 23:14
... alternatively you can change the datestyle setting with
SET datestyle TO dmy;
(postgresql.org/docs/9.4/…)– mkopriva
Nov 21 '18 at 23:14
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13
add a comment |
2 Answers
2
active
oldest
votes
I can see that datetime
variable is string
type. I don't know why you must convert time.Time
to string
before exec query.
If define date
column in readings
table is TIMESTAMP
, you can exec query like thisDb.Exec(SqlStatement, currentTime, temp, humi)
add a comment |
Solved it by adding
SET datestyle TO dmy;
Before each
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
So now I have two Db.Exec functions in a row, one is sending "SET datestyle TO dmy" and the other is this one that is sending actual data.
Wierd.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421338%2fhow-to-insert-timestamp-to-postgresql-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can see that datetime
variable is string
type. I don't know why you must convert time.Time
to string
before exec query.
If define date
column in readings
table is TIMESTAMP
, you can exec query like thisDb.Exec(SqlStatement, currentTime, temp, humi)
add a comment |
I can see that datetime
variable is string
type. I don't know why you must convert time.Time
to string
before exec query.
If define date
column in readings
table is TIMESTAMP
, you can exec query like thisDb.Exec(SqlStatement, currentTime, temp, humi)
add a comment |
I can see that datetime
variable is string
type. I don't know why you must convert time.Time
to string
before exec query.
If define date
column in readings
table is TIMESTAMP
, you can exec query like thisDb.Exec(SqlStatement, currentTime, temp, humi)
I can see that datetime
variable is string
type. I don't know why you must convert time.Time
to string
before exec query.
If define date
column in readings
table is TIMESTAMP
, you can exec query like thisDb.Exec(SqlStatement, currentTime, temp, humi)
answered Nov 22 '18 at 3:12
KibGzrKibGzr
1,466610
1,466610
add a comment |
add a comment |
Solved it by adding
SET datestyle TO dmy;
Before each
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
So now I have two Db.Exec functions in a row, one is sending "SET datestyle TO dmy" and the other is this one that is sending actual data.
Wierd.
add a comment |
Solved it by adding
SET datestyle TO dmy;
Before each
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
So now I have two Db.Exec functions in a row, one is sending "SET datestyle TO dmy" and the other is this one that is sending actual data.
Wierd.
add a comment |
Solved it by adding
SET datestyle TO dmy;
Before each
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
So now I have two Db.Exec functions in a row, one is sending "SET datestyle TO dmy" and the other is this one that is sending actual data.
Wierd.
Solved it by adding
SET datestyle TO dmy;
Before each
_, err = Db.Exec(SqlStatement, datetime, temp, humi)
So now I have two Db.Exec functions in a row, one is sending "SET datestyle TO dmy" and the other is this one that is sending actual data.
Wierd.
answered Nov 22 '18 at 12:16
RobertRobert
104
104
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421338%2fhow-to-insert-timestamp-to-postgresql-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's the
21
that's out of range because postgres timestamps are by default MM-DD-YYYY ... . So just switch 21 with 11 and you're good to go.– mkopriva
Nov 21 '18 at 23:07
... alternatively you can change the datestyle setting with
SET datestyle TO dmy;
(postgresql.org/docs/9.4/…)– mkopriva
Nov 21 '18 at 23:14
@mkopriva please put in proper form not in comment.. :)
– dwir182
Nov 22 '18 at 1:21
@mkopriva I did that already before so I tought it was not a problem, BUT it turns out that I have to execute that sql before each write to the db. Wierd but it works now.
– Robert
Nov 22 '18 at 12:13