Files
genai-toolbox/docs/en/getting-started/quickstart/shared/database_setup.md
Harsh Jha e376cce18c docs: add shortcode for including file regions (#1043)
Introduces a new Hugo shortcode, `includeRegion`, to allow embedding
specific, tagged regions from source files directly into Markdown
content.

This helps maintain a single source of truth for code snippets and other
repeated content, such as setup instructions, preventing duplication and
simplifying updates across multiple quickstart guides.

Demo PR: https://github.com/googleapis/genai-toolbox/pull/1179

---------

Co-authored-by: Anmol Shukla <shuklaanmol@google.com>
2025-08-22 06:24:50 +00:00

4.2 KiB

In this section, we will create a database, insert some data that needs to be accessed by our agent, and create a database user for Toolbox to connect with.

  1. Connect to postgres using the psql command:

    psql -h 127.0.0.1 -U postgres
    

    Here, postgres denotes the default postgres superuser.

    {{< notice info >}}

Having trouble connecting?

  • Password Prompt: If you are prompted for a password for the postgres user and do not know it (or a blank password doesn't work), your PostgreSQL installation might require a password or a different authentication method.
  • FATAL: role "postgres" does not exist: This error means the default postgres superuser role isn't available under that name on your system.
  • Connection refused: Ensure your PostgreSQL server is actually running. You can typically check with sudo systemctl status postgresql and start it with sudo systemctl start postgresql on Linux systems.

Common Solution

For password issues or if the postgres role seems inaccessible directly, try switching to the postgres operating system user first. This user often has permission to connect without a password for local connections (this is called peer authentication).

sudo -i -u postgres
psql -h 127.0.0.1

Once you are in the psql shell using this method, you can proceed with the database creation steps below. Afterwards, type \q to exit psql, and then exit to return to your normal user shell.

If desired, once connected to psql as the postgres OS user, you can set a password for the postgres database user using: ALTER USER postgres WITH PASSWORD 'your_chosen_password';. This would allow direct connection with -U postgres and a password next time. {{< /notice >}}

  1. Create a new database and a new user:

    {{< notice tip >}} For a real application, it's best to follow the principle of least permission and only grant the privileges your application needs. {{< /notice >}}

      CREATE USER toolbox_user WITH PASSWORD 'my-password';
    
      CREATE DATABASE toolbox_db;
      GRANT ALL PRIVILEGES ON DATABASE toolbox_db TO toolbox_user;
    
      ALTER DATABASE toolbox_db OWNER TO toolbox_user;
    
  2. End the database session:

    \q
    

    (If you used sudo -i -u postgres and then psql, remember you might also need to type exit after \q to leave the postgres user's shell session.)

  3. Connect to your database with your new user:

    psql -h 127.0.0.1 -U toolbox_user -d toolbox_db
    
  4. Create a table using the following command:

    CREATE TABLE hotels(
      id            INTEGER NOT NULL PRIMARY KEY,
      name          VARCHAR NOT NULL,
      location      VARCHAR NOT NULL,
      price_tier    VARCHAR NOT NULL,
      checkin_date  DATE    NOT NULL,
      checkout_date DATE    NOT NULL,
      booked        BIT     NOT NULL
    );
    
  5. Insert data into the table.

    INSERT INTO hotels(id, name, location, price_tier, checkin_date, checkout_date, booked)
    VALUES
      (1, 'Hilton Basel', 'Basel', 'Luxury', '2024-04-22', '2024-04-20', B'0'),
      (2, 'Marriott Zurich', 'Zurich', 'Upscale', '2024-04-14', '2024-04-21', B'0'),
      (3, 'Hyatt Regency Basel', 'Basel', 'Upper Upscale', '2024-04-02', '2024-04-20', B'0'),
      (4, 'Radisson Blu Lucerne', 'Lucerne', 'Midscale', '2024-04-24', '2024-04-05', B'0'),
      (5, 'Best Western Bern', 'Bern', 'Upper Midscale', '2024-04-23', '2024-04-01', B'0'),
      (6, 'InterContinental Geneva', 'Geneva', 'Luxury', '2024-04-23', '2024-04-28', B'0'),
      (7, 'Sheraton Zurich', 'Zurich', 'Upper Upscale', '2024-04-27', '2024-04-02', B'0'),
      (8, 'Holiday Inn Basel', 'Basel', 'Upper Midscale', '2024-04-24', '2024-04-09', B'0'),
      (9, 'Courtyard Zurich', 'Zurich', 'Upscale', '2024-04-03', '2024-04-13', B'0'),
      (10, 'Comfort Inn Bern', 'Bern', 'Midscale', '2024-04-04', '2024-04-16', B'0');
    
  6. End the database session:

    \q