Skip to content

Commit d4f8cb7

Browse files
Closes #18780: External database configuration (#18912)
1 parent f69de12 commit d4f8cb7

File tree

7 files changed

+94
-49
lines changed

7 files changed

+94
-49
lines changed

docs/configuration/required-parameters.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,30 @@ ALLOWED_HOSTS = ['*']
2525

2626
## DATABASE
2727

28-
NetBox requires access to a PostgreSQL 14 or later database service to store data. This service can run locally on the NetBox server or on a remote system. The following parameters must be defined within the `DATABASE` dictionary:
28+
!!! warning "Legacy Configuration Parameter"
29+
The `DATABASE` configuration parameter is deprecated and will be removed in a future release. Users are advised to adopt the new `DATABASES` (plural) parameter, which allows for the configuration of multiple databases.
30+
31+
See the [`DATABASES`](#databases) configuration below for usage.
32+
33+
---
34+
35+
## DATABASES
36+
37+
!!! info "This parameter was introduced in NetBox v4.3."
38+
39+
NetBox requires access to a PostgreSQL 14 or later database service to store data. This service can run locally on the NetBox server or on a remote system. Databases are defined as named dictionaries:
40+
41+
```python
42+
DATABASES = {
43+
'default': {...},
44+
'external1': {...},
45+
'external2': {...},
46+
}
47+
```
48+
49+
NetBox itself requires only that a `default` database is defined. However, certain plugins may require the configuration of additional databases. (Consider also configuring the [`DATABASE_ROUTERS`](./system.md#database_routers) parameter when multiple databases are in use.)
50+
51+
The following parameters must be defined for each database:
2952

3053
* `NAME` - Database name
3154
* `USER` - PostgreSQL username
@@ -38,22 +61,24 @@ NetBox requires access to a PostgreSQL 14 or later database service to store dat
3861
Example:
3962

4063
```python
41-
DATABASE = {
42-
'ENGINE': 'django.db.backends.postgresql',
43-
'NAME': 'netbox', # Database name
44-
'USER': 'netbox', # PostgreSQL username
45-
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
46-
'HOST': 'localhost', # Database server
47-
'PORT': '', # Database port (leave blank for default)
48-
'CONN_MAX_AGE': 300, # Max database connection age
64+
DATABASES = {
65+
'default': {
66+
'ENGINE': 'django.db.backends.postgresql',
67+
'NAME': 'netbox', # Database name
68+
'USER': 'netbox', # PostgreSQL username
69+
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
70+
'HOST': 'localhost', # Database server
71+
'PORT': '', # Database port (leave blank for default)
72+
'CONN_MAX_AGE': 300, # Max database connection age
73+
}
4974
}
5075
```
5176

5277
!!! note
5378
NetBox supports all PostgreSQL database options supported by the underlying Django framework. For a complete list of available parameters, please see [the Django documentation](https://docs.djangoproject.com/en/stable/ref/settings/#databases).
5479

5580
!!! warning
56-
Make sure to use a PostgreSQL-compatible backend for the ENGINE setting. If you don't specify an ENGINE, the default will be django.db.backends.postgresql.
81+
The `ENGINE` parameter must specify a PostgreSQL-compatible database backend. If not defined, the default engine `django.db.backends.postgresql` will be used.
5782

5883
---
5984

docs/configuration/system.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ BASE_PATH = 'netbox/'
1212

1313
---
1414

15+
## DATABASE_ROUTERS
16+
17+
Default: `[]` (empty list)
18+
19+
An iterable of [database routers](https://docs.djangoproject.com/en/stable/topics/db/multi-db/) to use for automatically selecting the appropriate database(s) for a query. This is useful only when [multiple databases](./required-parameters.md#databases) have been configured.
20+
21+
---
22+
1523
## DEFAULT_LANGUAGE
1624

1725
Default: `en-us` (US English)

docs/development/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ You may also need to set up the yarn packages as shown in the [Web UI Developmen
115115
Within the `netbox/netbox/` directory, copy `configuration_example.py` to `configuration.py` and update the following parameters:
116116

117117
* `ALLOWED_HOSTS`: This can be set to `['*']` for development purposes
118-
* `DATABASE`: PostgreSQL database connection parameters
118+
* `DATABASES`: PostgreSQL database connection parameters
119119
* `REDIS`: Redis configuration (if different from the defaults)
120120
* `SECRET_KEY`: Set to a random string (use `generate_secret_key.py` in the parent directory to generate a suitable key)
121121
* `DEBUG`: Set to `True`

docs/installation/3-netbox.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ sudo cp configuration_example.py configuration.py
128128
Open `configuration.py` with your preferred editor to begin configuring NetBox. NetBox offers [many configuration parameters](../configuration/index.md), but only the following four are required for new installations:
129129

130130
* `ALLOWED_HOSTS`
131-
* `DATABASE`
131+
* `DATABASES` (or `DATABASE`)
132132
* `REDIS`
133133
* `SECRET_KEY`
134134

@@ -146,18 +146,22 @@ If you are not yet sure what the domain name and/or IP address of the NetBox ins
146146
ALLOWED_HOSTS = ['*']
147147
```
148148

149-
### DATABASE
149+
### DATABASES
150150

151-
This parameter holds the database configuration details. You must define the username and password used when you configured PostgreSQL. If the service is running on a remote host, update the `HOST` and `PORT` parameters accordingly. See the [configuration documentation](../configuration/required-parameters.md#database) for more detail on individual parameters.
151+
This parameter holds the PostgreSQL database configuration details. The default database must be defined; additional databases may be defined as needed e.g. by plugins.
152+
153+
A username and password must be defined for the default database. If the service is running on a remote host, update the `HOST` and `PORT` parameters accordingly. See the [configuration documentation](../configuration/required-parameters.md#databases) for more detail on individual parameters.
152154

153155
```python
154-
DATABASE = {
155-
'NAME': 'netbox', # Database name
156-
'USER': 'netbox', # PostgreSQL username
157-
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
158-
'HOST': 'localhost', # Database server
159-
'PORT': '', # Database port (leave blank for default)
160-
'CONN_MAX_AGE': 300, # Max database connection age (seconds)
156+
DATABASES = {
157+
'default': {
158+
'NAME': 'netbox', # Database name
159+
'USER': 'netbox', # PostgreSQL username
160+
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
161+
'HOST': 'localhost', # Database server
162+
'PORT': '', # Database port (leave blank for default)
163+
'CONN_MAX_AGE': 300, # Max database connection age (seconds)
164+
}
161165
}
162166
```
163167

netbox/netbox/configuration_example.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212

1313
# PostgreSQL database configuration. See the Django documentation for a complete list of available parameters:
1414
# https://docs.djangoproject.com/en/stable/ref/settings/#databases
15-
DATABASE = {
16-
'ENGINE': 'django.db.backends.postgresql', # Database engine
17-
'NAME': 'netbox', # Database name
18-
'USER': '', # PostgreSQL username
19-
'PASSWORD': '', # PostgreSQL password
20-
'HOST': 'localhost', # Database server
21-
'PORT': '', # Database port (leave blank for default)
22-
'CONN_MAX_AGE': 300, # Max database connection age
15+
DATABASES = {
16+
'default': {
17+
'ENGINE': 'django.db.backends.postgresql', # Database engine
18+
'NAME': 'netbox', # Database name
19+
'USER': '', # PostgreSQL username
20+
'PASSWORD': '', # PostgreSQL password
21+
'HOST': 'localhost', # Database server
22+
'PORT': '', # Database port (leave blank for default)
23+
'CONN_MAX_AGE': 300, # Max database connection age
24+
}
2325
}
2426

2527
# Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate

netbox/netbox/configuration_testing.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
ALLOWED_HOSTS = ['*']
77

8-
DATABASE = {
9-
'NAME': 'netbox',
10-
'USER': 'netbox',
11-
'PASSWORD': 'netbox',
12-
'HOST': 'localhost',
13-
'PORT': '',
14-
'CONN_MAX_AGE': 300,
8+
DATABASES = {
9+
'default': {
10+
'NAME': 'netbox',
11+
'USER': 'netbox',
12+
'PASSWORD': 'netbox',
13+
'HOST': 'localhost',
14+
'PORT': '',
15+
'CONN_MAX_AGE': 300,
16+
}
1517
}
1618

1719
PLUGINS = [

netbox/netbox/settings.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@
5353
)
5454
raise
5555

56-
# Check for missing required configuration parameters
57-
for parameter in ('ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY', 'REDIS'):
56+
# Check for missing/conflicting required configuration parameters
57+
for parameter in ('ALLOWED_HOSTS', 'SECRET_KEY', 'REDIS'):
5858
if not hasattr(configuration, parameter):
5959
raise ImproperlyConfigured(f"Required parameter {parameter} is missing from configuration.")
60+
if not hasattr(configuration, 'DATABASE') and not hasattr(configuration, 'DATABASES'):
61+
raise ImproperlyConfigured("The database configuration must be defined using DATABASE or DATABASES.")
62+
elif hasattr(configuration, 'DATABASE') and hasattr(configuration, 'DATABASES'):
63+
raise ImproperlyConfigured("DATABASE and DATABASES may not be set together. The use of DATABASES is encouraged.")
6064

6165
# Set static config parameters
6266
ADMINS = getattr(configuration, 'ADMINS', [])
@@ -84,7 +88,9 @@
8488
CSRF_COOKIE_SECURE = getattr(configuration, 'CSRF_COOKIE_SECURE', False)
8589
CSRF_TRUSTED_ORIGINS = getattr(configuration, 'CSRF_TRUSTED_ORIGINS', [])
8690
DATA_UPLOAD_MAX_MEMORY_SIZE = getattr(configuration, 'DATA_UPLOAD_MAX_MEMORY_SIZE', 2621440)
87-
DATABASE = getattr(configuration, 'DATABASE') # Required
91+
DATABASE = getattr(configuration, 'DATABASE', None) # Legacy DB definition
92+
DATABASE_ROUTERS = getattr(configuration, 'DATABASE_ROUTERS', [])
93+
DATABASES = getattr(configuration, 'DATABASES', {'default': DATABASE})
8894
DEBUG = getattr(configuration, 'DEBUG', False)
8995
DEFAULT_DASHBOARD = getattr(configuration, 'DEFAULT_DASHBOARD', None)
9096
DEFAULT_PERMISSIONS = getattr(configuration, 'DEFAULT_PERMISSIONS', {
@@ -220,17 +226,15 @@
220226
# Database
221227
#
222228

223-
# Set the database engine
224-
if 'ENGINE' not in DATABASE:
225-
if METRICS_ENABLED:
226-
DATABASE.update({'ENGINE': 'django_prometheus.db.backends.postgresql'})
227-
else:
228-
DATABASE.update({'ENGINE': 'django.db.backends.postgresql'})
229+
# Verify that a default database has been configured
230+
if 'default' not in DATABASES:
231+
raise ImproperlyConfigured("No default database has been configured.")
229232

230-
# Define the DATABASES setting for Django
231-
DATABASES = {
232-
'default': DATABASE,
233-
}
233+
# Set the database engine
234+
if 'ENGINE' not in DATABASES['default']:
235+
DATABASES['default'].update({
236+
'ENGINE': 'django_prometheus.db.backends.postgresql' if METRICS_ENABLED else 'django.db.backends.postgresql'
237+
})
234238

235239

236240
#

0 commit comments

Comments
 (0)