Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl 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/