External database

Configure an external database to warehouse your logs

We provide a Velvet hosted database to get started right away. It's free up to 100k requests per month.

Many teams prefer to warehouse logs to their own database once features are in production. This setup is ideal for products at scale, compliance requirements, and if you want to use logs directly in your workflow.

Request access to add an external database here.

Once your database is configured on the backend, all logs will be warehoused to this database.


How to create a PostgreSQL database

If you don't already have a database, create a new one.

*️⃣

Admin user

Run these commands as your admin user

Create the new velvet database

CREATE DATABASE velvet;

Create the velvet_user role and permissions to only allow the user to manage the velvet db

-- Create the role
CREATE ROLE velvet_user WITH LOGIN PASSWORD 'your_secure_password';

-- Grant privileges on the database
GRANT ALL PRIVILEGES ON DATABASE velvet TO velvet_user;

-- Grant usage and create privileges on the schema
GRANT USAGE, CREATE ON SCHEMA public TO velvet_user;

-- Grant privileges on existing objects
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO velvet_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO velvet_user;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO velvet_user;
GRANT TRIGGER ON ALL TABLES IN SCHEMA public TO velvet_user;

-- Grant privileges on future objects
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO velvet_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO velvet_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO velvet_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT TRIGGER ON TABLES TO velvet_user;

Create the table, function, and trigger

*️⃣

Velvet user

Run these commands as the velvet_user

Create the table, function, and trigger with the velvet_user so they implicitly have access to the database objects.

Create the llm_logs table.

create table if not exists llm_logs (
  id text primary key not null,
  serial_id serial not null,
  request jsonb,
  response jsonb,
  metadata jsonb,
  created_at timestamp with time zone default now(),
  updated_at timestamp with time zone
);

Create function update_timestamps. This provides a handy, uniform way of setting the updated_at column.

create or replace function update_timestamps()
returns trigger
as $$
begin
  new.updated_at = now();
  return new;
end;
$$
language plpgsql
security definer;

Create trigger for update_timestamps. This sets a trigger to run update_timestamps() on llm_logs table.

drop trigger if exists llm_logs_update_timestamps on llm_logs;

create trigger llm_logs_update_timestamps
before update on llm_logs
for each row
execute procedure update_timestamps();

Share your database connection string with Velvet

DATABASE_URI=postgresql://velvet_user:your_password@yourdbhost:5432/postgres

Securely share your URI with the Velvet engineering team using a password management tool like 1Password.

We'll get your workspace configured from here.


Request access

Request access to external database logging here. Once your database is configured on the backend, all logs will be warehoused to this database.

Email [email protected] with any questions.