0
# Database Operations
1
2
Direct database operations including schema synchronization, raw query execution, database introspection, and seeding with support for multiple database providers.
3
4
## Capabilities
5
6
### Database Pull
7
8
Pull database schema from existing database and generate Prisma schema file with models, relations, and constraints.
9
10
```bash { .api }
11
/**
12
* Pull schema from existing database to generate Prisma schema
13
* Introspects database structure and creates/updates schema.prisma
14
*/
15
prisma db pull [options]
16
17
Options:
18
--schema <path> Custom schema file path
19
--url <connection> Database connection URL
20
--force Overwrite existing schema without confirmation
21
--print Print schema to stdout instead of writing file
22
--help/-h Show db pull command help
23
```
24
25
**Usage Examples:**
26
27
```bash
28
# Pull schema from database specified in schema
29
prisma db pull
30
31
# Pull with custom schema location
32
prisma db pull --schema ./custom/schema.prisma
33
34
# Pull with direct connection URL
35
prisma db pull --url "postgresql://user:pass@localhost:5432/mydb"
36
37
# Preview schema without writing file
38
prisma db pull --print
39
40
# Force overwrite existing schema
41
prisma db pull --force
42
```
43
44
### Database Push
45
46
Push Prisma schema changes directly to the database without creating migration files, ideal for prototyping and development.
47
48
```bash { .api }
49
/**
50
* Push schema changes directly to database
51
* Synchronizes database with schema without migration files
52
*/
53
prisma db push [options]
54
55
Options:
56
--schema <path> Custom schema file path
57
--force-reset Reset database if schema changes cannot be applied
58
--accept-data-loss Accept potential data loss from schema changes
59
--skip-generate Skip automatic Prisma Client generation
60
--help/-h Show db push command help
61
```
62
63
**Usage Examples:**
64
65
```bash
66
# Push schema changes to database
67
prisma db push
68
69
# Push with potential data loss acceptance
70
prisma db push --accept-data-loss
71
72
# Push and reset database if needed
73
prisma db push --force-reset
74
75
# Push without regenerating client
76
prisma db push --skip-generate
77
```
78
79
### Database Execute
80
81
Execute raw SQL queries directly against the database with support for files and stdin input.
82
83
```bash { .api }
84
/**
85
* Execute raw SQL queries against the database
86
* Supports direct queries, files, and stdin input
87
*/
88
prisma db execute [options]
89
90
Options:
91
--schema <path> Custom schema file path
92
--url <connection> Database connection URL
93
--file <path> SQL file to execute
94
--stdin Read SQL from stdin
95
--help/-h Show db execute command help
96
```
97
98
**Usage Examples:**
99
100
```bash
101
# Execute SQL from file
102
prisma db execute --file ./scripts/seed.sql
103
104
# Execute SQL from stdin
105
echo "SELECT * FROM users;" | prisma db execute --stdin
106
107
# Execute with custom connection
108
prisma db execute --file ./query.sql --url "postgresql://..."
109
110
# Execute with custom schema
111
prisma db execute --file ./migration.sql --schema ./custom/schema.prisma
112
```
113
114
### Database Seed
115
116
Run database seeding scripts to populate database with initial or test data.
117
118
```bash { .api }
119
/**
120
* Run database seeding scripts
121
* Executes seed script defined in package.json or default locations
122
*/
123
prisma db seed [options]
124
125
Options:
126
--schema <path> Custom schema file path
127
--help/-h Show db seed command help
128
```
129
130
**Seed Configuration:**
131
132
```json
133
// package.json
134
{
135
"prisma": {
136
"seed": "tsx prisma/seed.ts"
137
}
138
}
139
```
140
141
**Usage Examples:**
142
143
```bash
144
# Run seed script
145
prisma db seed
146
147
# Run seed with custom schema
148
prisma db seed --schema ./custom/schema.prisma
149
```
150
151
**Seed Script Example:**
152
153
```typescript
154
// prisma/seed.ts
155
import { PrismaClient } from '@prisma/client'
156
157
const prisma = new PrismaClient()
158
159
async function main() {
160
// Create seed data
161
const alice = await prisma.user.create({
162
data: {
163
name: 'Alice',
164
email: 'alice@prisma.io',
165
posts: {
166
create: {
167
title: 'Hello World',
168
content: 'This is my first post',
169
published: true,
170
},
171
},
172
},
173
})
174
175
const bob = await prisma.user.create({
176
data: {
177
name: 'Bob',
178
email: 'bob@prisma.io',
179
posts: {
180
create: [
181
{
182
title: 'I am Bob',
183
content: 'This is my bio',
184
published: true,
185
},
186
{
187
title: 'Draft Post',
188
content: 'This is a draft',
189
published: false,
190
},
191
],
192
},
193
},
194
})
195
196
console.log({ alice, bob })
197
}
198
199
main()
200
.catch((e) => {
201
console.error(e)
202
process.exit(1)
203
})
204
.finally(async () => {
205
await prisma.$disconnect()
206
})
207
```
208
209
## Database Provider Support
210
211
### PostgreSQL Operations
212
213
```bash
214
# PostgreSQL-specific examples
215
prisma db pull --url "postgresql://user:password@localhost:5432/mydb"
216
prisma db push --schema ./postgresql-schema.prisma
217
prisma db execute --file ./postgresql-queries.sql
218
```
219
220
### MySQL Operations
221
222
```bash
223
# MySQL-specific examples
224
prisma db pull --url "mysql://user:password@localhost:3306/mydb"
225
prisma db push --schema ./mysql-schema.prisma
226
prisma db execute --file ./mysql-queries.sql
227
```
228
229
### SQLite Operations
230
231
```bash
232
# SQLite-specific examples
233
prisma db pull --url "file:./dev.db"
234
prisma db push --schema ./sqlite-schema.prisma
235
prisma db execute --file ./sqlite-queries.sql
236
```
237
238
### MongoDB Operations
239
240
```bash
241
# MongoDB-specific examples (limited operations)
242
prisma db pull --url "mongodb://localhost:27017/mydb"
243
prisma db push --schema ./mongodb-schema.prisma
244
```
245
246
## Schema Synchronization Patterns
247
248
### Development Workflow
249
250
```bash
251
# Typical development flow
252
prisma db push # Sync schema changes quickly
253
prisma generate # Regenerate client
254
prisma db seed # Populate with test data
255
```
256
257
### Production Introspection
258
259
```bash
260
# Pull production schema for local development
261
prisma db pull --url $PRODUCTION_DATABASE_URL --print > production-schema.prisma
262
```
263
264
### Schema Migration
265
266
```bash
267
# Convert push-based development to migrations
268
prisma db push # Final push of current schema
269
prisma migrate dev --create-only # Create migration from current state
270
```
271
272
## Error Handling
273
274
Common database operation errors:
275
276
- **Connection Errors**: Invalid database URL or connection issues
277
- **Permission Errors**: Insufficient database privileges
278
- **Schema Conflicts**: Conflicting schema changes during push
279
- **Data Loss**: Schema changes that would lose data
280
- **Syntax Errors**: Invalid SQL in execute operations
281
- **Seed Failures**: Errors during seeding process
282
283
## Integration Patterns
284
285
### CI/CD Integration
286
287
```bash
288
# Production deployment
289
prisma db push --accept-data-loss # For staging environments
290
prisma migrate deploy # For production environments
291
```
292
293
### Local Development
294
295
```bash
296
# Quick iteration cycle
297
prisma db push && prisma generate && prisma db seed
298
```
299
300
### Testing Integration
301
302
```bash
303
# Test database setup
304
DATABASE_URL="postgresql://test:test@localhost:5432/test_db" prisma db push
305
DATABASE_URL="postgresql://test:test@localhost:5432/test_db" prisma db seed
306
```