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

# PostgreSQL

> PostgreSQL can be automatically configured by Devbox via the built-in Postgres Plugin. This plugin will activate automatically when you install Postgres using `devbox add postgresql`.

[**Example Repo**](https://github.com/jetify-com/devbox/tree/main/examples/databases/postgres)

## Adding Postgres to your Shell[​](#adding-postgres-to-your-shell "Direct link to Adding Postgres to your Shell")

You can install the PostgreSQL server and client by running`devbox add postgresql`. In some Linux
distributions, you may also need to add `glibcLocales`, which can be added using
`devbox add glibcLocales --platform=x86_64-linux,aarch64-linux`.

Alternatively, you can add the following to your devbox.json:

```json theme={null}
  "packages": {
    "postgresql": "latest",
    "glibcLocales": {
      "version": "latest",
      "platforms": ["x86_64-linux", "aarch64-linux"]
    }
  }
```

This will install the latest version of Postgres. You can find other installable versions of
Postgres by running `devbox search postgresql`. You can also view the available versions on
[Nixhub](https://www.nixhub.io/packages/postgresql)

## PostgreSQL Plugin Support[​](#postgresql-plugin-support "Direct link to PostgreSQL Plugin Support")

Devbox will automatically create the following configuration when you run `devbox add postgresql`:

### Services[​](#services "Direct link to Services")

* postgresql

You can use `devbox services start|stop postgresql` to start or stop the Postgres server in the
background.

### Environment Variables[​](#environment-variables "Direct link to Environment Variables")

`PGHOST=./.devbox/virtenv/postgresql` `PGDATA=./.devbox/virtenv/postgresql/data`

This variable tells PostgreSQL which directory to use for creating and storing databases.

### NOTES[​](#notes "Direct link to NOTES")

1. To initialize PostgreSQL run:

```bash theme={null}
initdb
```

1. You also need to create a user using:

```bash theme={null}
createuser --interactive
```

1. (OPTIONAL) If the user has no permissions to create or drop a database, you also need to create a
   database using:

```bash theme={null}
createdb <db-name>
```

#### Using the `createuser` Command[​](#using-the-createuser-command "Direct link to using-the-createuser-command")

Run the createuser command with the `-s` or `--superuser` option to create a superuser. This grants
the user the ability to bypass all access permission checks within the database, effectively
granting them extensive privileges including the ability to create and drop databases. Additionally,
you can use the `-r` or `--createrole` option to allow the user to create new roles. Here's an
example command:

```bash theme={null}
createuser -s -r your_new_user_name
```

Replace `your_new_user_name` with the desired username for the new superuser.

Remember: Creating a superuser grants them significant power over the database system, so it should
be done cautiously and only when absolutely necessary due to the potential security implications.

### Disabling the Postgres Plugin[​](#disabling-the-postgres-plugin "Direct link to Disabling the Postgres Plugin")

You can disable the Postgres plugin by running `devbox add postgresql --disable-plugin`, or by
setting the `disable_plugin` field to `true` in your package definition:

```json theme={null}
{
  "packages": {
    "postgresql": {
      "version": "latest",
      "disable_plugin": true
    }
  }
}
```

[Edit this page](https://github.com/jetify-com/docs/tree/main/docs/devbox/devbox-examples/databases/postgres/index.mdx)
