Create tables in postgres

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    define tables in an already created db

    use postgres::{Client, NoTls, Error};
    
    fn main() -> Result<(), Error> {
      let mut client = Client::connect("postgresql://postgres:postgres@localhost/library", NoTls)?;
      client.batch_execute("
        CREATE TABLE IF NOT EXISTS author (
        id              SERIAL PRIMARY KEY,
        name            VARCHAR NOT NULL,
        country         VARCHAR NOT NULL
        )
        ")?;
      client.batch_execute("
        CREATE TABLE IF NOT EXISTS book  (
        id              SERIAL PRIMARY KEY,
        title           VARCHAR NOT NULL,
        author_id       INTEGER NOT NULL REFERENCES author
        )
        ")?;
      Ok(())
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.