Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a PostgreSQL timestamp to String?

I am getting this error when I try to retreive data from a database:

thread 'main' panicked at 'error retrieving column 2: error deserializing column 2: cannot convert between the Rust type `alloc::string::String` and the Postgres type `timestamp`'

Db structure:

CREATE TABLE IF NOT EXISTS table_(
            id SERIAL PRIMARY KEY,
            data VARCHAR NOT NULL,
            date_saved TIMESTAMP
        )
struct MyType{
    local_id: i32,
    data: String,
    date_saved: String
}

let records = client.query("SELECT id,data,date_saved FROM table_",&[])?;
let mut the_records : Vec<MyType> = vec![];
for record in records {
    let saved_data = MyType {
        local_id: record.get(0),
        data: record.get(1),
        date_saved: record.get(2),
    };
    println!("{:?}",saved_data.data);
    the_records.push(saved_data);
}
like image 700
Mathe Eliel Avatar asked Oct 14 '25 04:10

Mathe Eliel


1 Answers

I found out that there is no possible conversion between Postgres Timestamp and String according to https://docs.rs/postgres/0.17.5/postgres/types/trait.FromSql.html but we need to use std::time::SystemTime.

So MyType will be:

struct MyType{
    local_id: i32,
    data: String,
    date_saved: std::time::SystemTime
}

And I can manipulate time from there.

like image 184
Mathe Eliel Avatar answered Oct 18 '25 05:10

Mathe Eliel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!