> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hydra.so/llms.txt
> Use this file to discover all available pages before exploring further.

# Using an ORM

An ORM simplifies working with Hydra. It turns tables into objects, queries into code. Less SQL, more logic. ORMs unlock efficient data modeling and transaction management. Hydra is Postgres so all existing ORM integrations with Postgres are compatible. In this guide we will focus on Drizzle ORM, but feel free to connect with your preferred ORM by navigating to their Postgres documentation.

## Drizzle ORM

<Note>
  This guide assumes familiarity with:

  * Database [connection basics](https://orm.drizzle.team/docs/connect-overview) with Drizzle
  * node-postgres [basics](https://node-postgres.com/)
  * postgres.js [basics](https://github.com/porsager/postgres?tab=readme-ov-file#usage)
</Note>

Drizzle has native support for PostgreSQL connections with the `node-postgres` and `postgres.js` drivers.

There are a few differences between the `node-postgres` and `postgres.js` drivers that we discovered while using both and integrating them with the Drizzle ORM. For example:

* With `node-postgres`, you can install `pg-native` to boost the speed of both `node-postgres` and Drizzle by approximately 10%.
* `node-postgres` supports providing type parsers on a per-query basis without globally patching things. For more details, see [Types Docs](https://node-postgres.com/features/queries#types).
* `postgres.js` uses prepared statements by default, which you may need to opt out of.

## node-postgres

#### Step 1 - Install packages

<CodeGroup>
  ```javascript npm theme={null}
  npm i drizzle-orm pg
  npm i -D drizzle-kit @types/pg
  ```

  ```javascript yarn theme={null}
  yarn add drizzle-orm pg
  yarn add -D drizzle-kit @types/pg
  ```

  ```javascript pnpm theme={null}
  pnpm add drizzle-orm pg
  pnpm add -D drizzle-kit @types/pg
  ```

  ```javascript bun theme={null}
  bun add drizzle-orm pg
  bun add -D drizzle-kit @types/pg
  ```
</CodeGroup>

#### Step 2 - Initialize the driver and make a query

<CodeGroup>
  ```javascript node-postgres theme={null}
  // Make sure to install the 'pg' package 
  import { drizzle } from 'drizzle-orm/node-postgres';

  const db = drizzle(process.env.DATABASE_URL);
   
  const result = await db.execute('select 1');
  ```

  ```javascript node-postgres with config theme={null}
  // Make sure to install the 'pg' package 
  import { drizzle } from 'drizzle-orm/node-postgres';

  // You can specify any property from the node-postgres connection options
  const db = drizzle({ 
    connection: { 
      connectionString: process.env.DATABASE_URL,
      ssl: true
    }
  });
   
  const result = await db.execute('select 1');
  ```
</CodeGroup>

If you need to provide your existing driver:

```javascript theme={null}
// Make sure to install the 'pg' package 
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});
const db = drizzle({ client: pool });
 
const result = await db.execute('select 1');
```

## postgres.js

#### Step 1 - Install packages

<CodeGroup>
  ```javascript npm theme={null}
  npm i drizzle-orm postgres
  npm i -D drizzle-kit
  ```

  ```javascript yarn theme={null}
  yarn add drizzle-orm postgres
  yarn add -D drizzle-kit
  ```

  ```javascript pnpm theme={null}
  pnpm add drizzle-orm postgres
  pnpm add -D drizzle-kit
  ```

  ```javascript bun theme={null}
  bun add drizzle-orm postgres
  bun add -D drizzle-kit
  ```
</CodeGroup>

#### Step 2 - Initialize the driver and make a query

<CodeGroup>
  ```javascript postgres.js theme={null}
  import { drizzle } from 'drizzle-orm/postgres-js';

  const db = drizzle(process.env.DATABASE_URL);

  const result = await db.execute('select 1');
  ```

  ```javascript postgres.js with config theme={null}
  import { drizzle } from 'drizzle-orm/postgres-js';

  // You can specify any property from the postgres-js connection options
  const db = drizzle({ 
    connection: { 
      url: process.env.DATABASE_URL, 
      ssl: true 
    }
  });

  const result = await db.execute('select 1');
  ```
</CodeGroup>

If you need to provide your existing driver:

```javascript theme={null}
// Make sure to install the 'postgres' package
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const queryClient = postgres(process.env.DATABASE_URL);
const db = drizzle({ client: queryClient });

const result = await db.execute('select 1');
```

## What’s Next?

We recommend

* following the migration documentation from [Object Storage](/migration/data_lake) (S3, GCS), Amazon [RDS](/migration/rds), [Heroku](/migration/heroku), [Render](/migration/render), [Postgres](/migration/postgres).
* learning [about Hydra](/hydra/about), our team and partners behind the project
* scanning the Hydra [architecture ](/start/architecture)
