Categories
Tags
algorithms APIT arm assembly asynchronous base64 Blogging box c clang-format cmake compiler concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db Demo deserialization discrete doc DP Dynamic Example FFI flat_map FP Functional functions futures Fuwari GATs gccrs generics gitignore GUI hacking hashmap haskell heap interop invariant iterator justfile kernel LaTeX LFU linux MachineLearning Markdown math ML OnceLock optimization OS parallels perf physics pin postgresql release RPIT rust science Science serialization shift sin SmallProjects std String surrealdb swisstable synchronous tan traits triangulation utf16 utf8 Video x86_64 xilem zig
334 words
2 minutes
260215_PostgresSQL_backup_export_import
link
When you say “extract files to PostgreSQL DB files”, this can mean two different things:
- ✅ Import external files (CSV, JSON, etc.) into tables
- ✅ Extract/export data from PostgreSQL into files
- ⚠️ Directly modifying PostgreSQL’s internal database files (not recommended)
I’ll clearly separate them.
PostgreSQL backup(전체백업, Full physical DB copy)
pg_basebackup -D backup_folder -Fp -Xs -P1. Summary (Clear Difference)
| Goal | Command |
|---|---|
| CSV → Table | \copy FROM |
| Table → CSV | \copy TO |
| DB → SQL file | pg_dump |
| Full physical DB copy | pg_basebackup |
| Edit raw DB files | ❌ Never |
2. Where PostgreSQL Stores Its Real DB Files
PostgreSQL stores internal data files in its data directory.
On Linux typically:
/var/lib/postgresql/<version>/main/- On macOS (Homebrew):
/opt/homebrew/var/postgresql@<version>/- Inside you’ll see: (global에 저장 되나봄)
base/
global/
pg_wal/Visualizing Internal Storage
data_directory/
├── base/
│ ├── 16384/
│ │ ├── 12345
│ │ ├── 12346
│
├── global/
├── pg_wal/
2️⃣ Extract PostgreSQL Data → File
- Now the reverse direction.
csv로 export하기
- Server-side (faster but requires permission)
COPY users FROM '/absolute/path/data.csv' CSV HEADER;- The file must be accessible to the PostgreSQL server process.
Export Entire Database
- Use:
pg_dump
- Example:
pg_dump -U postgres mydb > backup.sql- This creates a logical SQL dump file.
Export Binary Backup (Faster, Production)
pg_dump -Fc mydb > backup.dump- Restore with:
pg_restore -U postgres -d mydb backup.dump3️⃣ Where PostgreSQL Stores Its Real DB Files
- PostgreSQL stores internal data files in its data directory.
3.1. Where PostgreSQL Stores Its Real DB Files
PostgreSQL stores internal data files in its data directory.
On Linux typically:
/var/lib/postgresql/<version>/main/- On macOS (Homebrew):
/opt/homebrew/var/postgresql@<version>/- Inside you’ll see: (global에 저장 되나봄)
base/
global/
pg_wal/3.2 Visualizing Internal Storage
data_directory/
├── base/
│ ├── 16384/
│ │ ├── 12345
│ │ ├── 12346
│
├── global/
├── pg_wal/
Each number represents:
- Database OID
- Table files
- WAL logs
⚠️ You should NEVER manually edit these files.
If you want to know the data directory:
SHOW data_directory;4️⃣ If You Meant “Extract Raw Database Files”
- PostgreSQL is different from file-based databases like:
- SQLite
- SQLite uses:
mydb.sqlitePostgreSQL does NOT work like that.
PostgreSQL is a server-based database.
You cannot simply copy one file to move a database.
Instead, use:
pg_dumppg_basebackup(for full physical backup)
5️⃣ Physical Backup (Real DB Files)
- To copy actual internal database files safely:
- Stop PostgreSQL first OR use:
pg_basebackup -D backup_folder -Fp -Xs -P- This copies real database storage safely.
260215_PostgresSQL_backup_export_import
https://younghakim7.github.io/blog/posts/260215_postgressql_backup_export_import/