0

Create a database

by
Published Apr 8, 2025

Creates a database in the specified branch. A branch can have multiple databases. You can obtain a `project_id` by listing the projects for your Neon account. You can obtain the `branch_id` by listing the project's branches. For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).

Script neondb Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Neondb = {
3
	apiKey: string
4
}
5
/**
6
 * Create a database
7
 * Creates a database in the specified branch.
8
A branch can have multiple databases.
9
You can obtain a `project_id` by listing the projects for your Neon account.
10
You can obtain the `branch_id` by listing the project's branches.
11
For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
12

13
 */
14
export async function main(
15
	auth: Neondb,
16
	project_id: string,
17
	branch_id: string,
18
	body: { database: { name: string; owner_name: string } }
19
) {
20
	const url = new URL(
21
		`https://console.neon.tech/api/v2/projects/${project_id}/branches/${branch_id}/databases`
22
	)
23

24
	const response = await fetch(url, {
25
		method: 'POST',
26
		headers: {
27
			'Content-Type': 'application/json',
28
			Authorization: 'Bearer ' + auth.apiKey
29
		},
30
		body: JSON.stringify(body)
31
	})
32
	if (!response.ok) {
33
		const text = await response.text()
34
		throw new Error(`${response.status} ${text}`)
35
	}
36
	return await response.json()
37
}
38