2020-03-05 17:23:56 -05:00
# PostgreSQL Database Installation
2020-02-26 11:40:31 -05:00
This section entails the installation and configuration of a local PostgreSQL database. If you already have a PostgreSQL database service in place, skip to [the next section ](2-redis.md ).
2017-06-12 10:06:19 -04:00
2023-06-22 12:27:21 -04:00
!!! warning "PostgreSQL 12 or later required"
NetBox requires PostgreSQL 12 or later. Please note that MySQL and other relational databases are **not** supported.
2020-02-26 11:40:31 -05:00
## Installation
2017-09-29 14:36:55 -04:00
2021-02-24 21:21:17 -05:00
=== "Ubuntu"
2017-09-29 14:36:55 -04:00
2021-02-24 21:21:17 -05:00
```no-highlight
sudo apt update
2021-08-08 10:19:30 +01:00
sudo apt install -y postgresql
2021-02-24 21:21:17 -05:00
```
2016-09-02 16:38:58 -04:00
2021-02-24 21:21:17 -05:00
=== "CentOS"
2016-07-08 16:00:53 -04:00
2021-02-24 21:21:17 -05:00
```no-highlight
2021-08-08 10:19:30 +01:00
sudo yum install -y postgresql-server
2021-02-24 21:21:17 -05:00
sudo postgresql-setup --initdb
```
2017-10-12 13:38:23 -04:00
2021-04-05 21:46:48 -04:00
CentOS configures ident host-based authentication for PostgreSQL by default. Because NetBox will need to authenticate using a username and password, modify `/var/lib/pgsql/data/pg_hba.conf` to support MD5 authentication by changing `ident` to `md5` for the lines below:
2016-09-02 16:38:58 -04:00
2021-04-05 21:46:48 -04:00
```no-highlight
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
```
2016-09-26 12:50:44 -04:00
2023-03-30 15:45:32 -04:00
Once PostgreSQL has been installed, start the service and enable it to run at boot:
2016-09-26 12:50:44 -04:00
2023-03-30 15:45:32 -04:00
```no-highlight
2024-02-23 15:56:14 -05:00
sudo systemctl enable --now postgresql
2023-03-30 15:45:32 -04:00
```
2016-07-08 16:00:53 -04:00
2023-06-22 12:27:21 -04:00
Before continuing, verify that you have installed PostgreSQL 12 or later:
2021-11-04 15:18:58 -04:00
```no-highlight
psql -V
```
2020-02-26 11:40:31 -05:00
## Database Creation
2016-07-08 16:00:53 -04:00
2021-08-30 14:15:21 -04:00
At a minimum, we need to create a database for NetBox and assign it a username and password for authentication. Start by invoking the PostgreSQL shell as the system Postgres user.
2016-07-08 16:00:53 -04:00
2016-11-30 12:07:51 -05:00
```no-highlight
2021-08-30 14:15:21 -04:00
sudo -u postgres psql
```
2016-07-08 16:00:53 -04:00
2021-08-30 14:15:21 -04:00
Within the shell, enter the following commands to create the database and user (role), substituting your own value for the password:
```postgresql
CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'J5brHrAXFLQSif0K';
2023-03-30 16:35:56 -04:00
ALTER DATABASE netbox OWNER TO netbox;
2023-07-12 23:05:35 +02:00
-- the next two commands are needed on PostgreSQL 15 and later
\connect netbox;
GRANT CREATE ON SCHEMA public TO netbox;
2016-07-08 16:00:53 -04:00
```
2021-11-04 15:18:58 -04:00
!!! danger "Use a strong password"
2021-08-30 14:15:21 -04:00
**Do not use the password from the example.** Choose a strong, random password to ensure secure database authentication for your NetBox installation.
Once complete, enter `\q` to exit the PostgreSQL shell.
2020-02-26 11:40:31 -05:00
## Verify Service Status
2021-08-30 14:15:21 -04:00
You can verify that authentication works by executing the `psql` command and passing the configured username and password. (Replace `localhost` with your database server if using a remote database.)
2016-07-08 16:00:53 -04:00
2016-11-30 12:07:51 -05:00
```no-highlight
2020-11-17 12:01:10 -05:00
$ psql --username netbox --password --host localhost netbox
2020-07-20 16:53:04 -04:00
Password for user netbox:
2020-11-17 12:01:10 -05:00
psql (12.5 (Ubuntu 12.5-0ubuntu0.20.04.1))
2020-07-20 16:53:04 -04:00
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
2020-11-17 12:01:10 -05:00
netbox=> \conninfo
You are connected to database "netbox" as user "netbox" on host "localhost" (address "127.0.0.1") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
2020-07-20 16:53:04 -04:00
netbox=> \q
2016-07-08 16:00:53 -04:00
```
2020-11-17 12:01:10 -05:00
If successful, you will enter a `netbox` prompt. Type `\conninfo` to confirm your connection, or type `\q` to exit.