ฐานข้อมูลจังหวัดประเทศไทย อำเภอ ตำบล และรหัสไปรษณีย์ พร้อม export เป็น JSON, CSV, YAML, SQL, MongoDB และ plain text สำหรับนำไปใช้ในเว็บ แอป API backend ระบบค้นหาที่อยู่ ระบบสมาชิก ระบบจัดส่ง หรือ seed database เริ่มต้นของโปรเจกต์
โปรเจกต์นี้ช่วยแปลงไฟล์ Excel ต้นทางของข้อมูลภูมิศาสตร์ไทยให้เป็น format ที่นำไปใช้งานต่อได้ง่าย ทั้งแบบรวม table เดียวและแบบ relational แยกเป็น provinces, districts, subdistricts, และ postal_codes
English README: README.en.md
- ฐานข้อมูลจังหวัดประเทศไทย
- ฐานข้อมูลจังหวัด อำเภอ ตำบล
- ฐานข้อมูลรหัสไปรษณีย์ไทย
- รายชื่อจังหวัด อำเภอ ตำบล ภาษาไทยและภาษาอังกฤษ
- Thai province district subdistrict database
- Thailand postal code database
- seed data จังหวัด อำเภอ ตำบล สำหรับ PostgreSQL, MySQL, MongoDB
- JSON / CSV / YAML รายชื่อจังหวัดไทย
แต่ละ record หลักประกอบด้วย:
- จังหวัด ภาษาไทยและภาษาอังกฤษ
- อำเภอ/เขต ภาษาไทยและภาษาอังกฤษ
- ตำบล/แขวง ภาษาไทยและภาษาอังกฤษ
- รหัสไปรษณีย์
- slug ของจังหวัด อำเภอ และตำบล
ตัวอย่าง canonical JSON:
{
"id": 1,
"province": "กระบี่",
"provinceEn": "Krabi",
"district": "เกาะลันตา",
"districtEn": "Ko Lanta",
"subdistrict": "เกาะกลาง",
"subdistrictEn": "Ko Klang",
"postalCode": "81120",
"provinceSlug": "krabi",
"districtSlug": "ko-lanta",
"subdistrictSlug": "ko-klang"
}รหัสไปรษณีย์เก็บเป็น string เสมอ และชื่อภาษาไทย/อังกฤษจะคงตามต้นฉบับ ยกเว้นการ normalize whitespace
ไฟล์แบบรวม:
dist/thai-postal-codes.jsondist/thai-postal-codes.jsonldist/thai-postal-codes.csvdist/thai-postal-codes.txtdist/thai-postal-codes.yaml
ไฟล์แบบแยก entity:
dist/relational/provinces.*dist/relational/districts.*dist/relational/subdistricts.*dist/relational/postal-codes.*
Database seed:
dist/sql/postgresql.sqldist/sql/postgresql-relational.sqldist/sql/mysql.sqldist/sql/mysql-relational.sqldist/mongodb/thai-postal-codes.mongodb.jsdist/mongodb/thai-postal-codes.relational.mongodb.js
ทุกครั้งที่ใช้ build ระบบจะล้าง output directory เดิมก่อนสร้างไฟล์ใหม่ เพื่อไม่ให้มีไฟล์เก่าค้างอยู่ใน dist/
ข้อมูลตั้งต้นนำมาจาก ThepExcel: ฐานข้อมูลตำบล อำเภอ จังหวัด รหัสไปรษณีย์ ของประเทศไทย V3
ไฟล์ Excel ตัวอย่างจาก ThepExcel:
https://github.com/ThepExcel/download/blob/master/ThepExcel-Thailand-Tambon.xlsx
โปรเจกต์นี้เป็นเครื่องมือแปลง format เพื่อให้นำข้อมูลไปใช้ต่อได้ง่ายขึ้น ไม่ใช่แหล่งข้อมูลราชการหรือ official government database โปรดตรวจสอบความถูกต้องกับแหล่งข้อมูลที่เหมาะสมก่อนใช้ในงานสำคัญ
npm installวางไฟล์ต้นฉบับไว้ที่:
sources/thai-postal-codes.xlsx
npm run extractหรือระบุ path เอง:
npx thai-geo-db extract --source sources/thai-postal-codes.xlsx --table "รหัสไปรษณีย์"Extractor จะอ่าน table/worksheet รหัสไปรษณีย์ และอ่าน TambonDatabase เพิ่มเพื่อ join ชื่อภาษาอังกฤษผ่าน TambonID
npm run build:outputsหรือเลือกเฉพาะ format:
npx thai-geo-db build --format json csv yaml
npx thai-geo-db build --format postgresql mysql mongodbค่า default คือ slug-case โดยใช้ชื่อภาษาอังกฤษก่อน ถ้าไม่มีจึง fallback เป็นภาษาไทย
เลือก style ได้:
npx thai-geo-db extract --slug-style slug-case
npx thai-geo-db extract --slug-style snake_case
npx thai-geo-db extract --slug-style camelCase
npx thai-geo-db extract --slug-style PascalCase
npx thai-geo-db extract --slug-style UPPER_UNDERSCORE_CASE
npx thai-geo-db extract --slug-style lowercase
npx thai-geo-db extract --slug-style UPPERCASEnpm run wizardWizard ใช้เลือก output format, output directory, slug style, SQL table name, SQL column name, relational table/collection name และตัวเลือก SQL เช่น DROP TABLE, CREATE TABLE, และ INSERT
ค่า default ของ DB output คือ both เพื่อสร้างทั้งแบบรวม table เดียวและแบบ relational
npx thai-geo-db build --format postgresql --db-layout both
npx thai-geo-db build --format postgresql --db-layout combined
npx thai-geo-db build --format postgresql --db-layout relationalแบบรวม table เดียว:
thai_postal_codes
แบบ relational:
provincesdistrictssubdistrictspostal_codes
ตัวอย่างเปลี่ยนชื่อ table/column สำหรับ output แบบรวม:
npx thai-geo-db build \
--format postgresql \
--table locations \
--column-postal-code zip_codeตัวอย่างเปลี่ยนชื่อ relational table หรือ MongoDB collection:
npx thai-geo-db build \
--format postgresql mongodb \
--db-layout relational \
--table-provinces geo_provinces \
--table-districts geo_districts \
--table-subdistricts geo_subdistricts \
--table-postal-codes geo_postal_codesค่า default ของ combined SQL:
- Table:
thai_postal_codes - Columns:
id,province,province_en,district,district_en,subdistrict,subdistrict_en,postal_code
npm testห้ามแก้ไฟล์ใน dist/ ด้วยมือ ให้แก้ logic ใน src/ แล้ว generate ใหม่
MIT
โค้ดชุดนี้พัฒนาโดย NakornCode โดยใช้ GPT-5.5 ช่วย generate และปรับปรุง code