# Repository: dr5hn/countries-states-cities-database
# Stars: 9442
## README.md

# ๐ Countries States Cities Database
[](LICENSE)
[](https://github.com/dr5hn/countries-states-cities-database/stargazers)
[](https://github.com/dr5hn/countries-states-cities-database/network)


Full Database of city state country available in **11+ formats**: JSON, MYSQL, PSQL, SQLITE, SQLSERVER, XML, YAML, MONGODB, CSV, GEOJSON, TOON & more.
All Countries, States & Cities are Covered & Populated with Different Combinations & Versions.
## Why Choose This Database?
* โ
**Most Comprehensive** - 153K+ cities from 250 countries with 100% timezone coverage & multilingual support (19 languages)
* โ
**Multiple Integration Options** - NPM/PyPI packages, REST API, Export Tool, or direct downloads
* โ
**Production Ready** - Trusted by thousands of developers, monthly updates
* โ
**Every Format You Need** - JSON, SQL, MongoDB, CSV, XML, YAML, GeoJSON, Toon - use what fits your stack
* โ
**100% Free & Open Source** - ODbL licensed, no usage restrictions, developer-friendly
Save hundreds of hours collecting and maintaining geographical data. Get accurate, structured, ready-to-use data right now.
> **๐ฆ Clone Tip:** Use `git clone --depth 1` for faster cloning. Large exports (.gz) are on [GitHub Releases](https://github.com/dr5hn/countries-states-cities-database/releases), not in git.
## Table of Contents
- [CSC Platform Ecosystem](#-csc-platform-ecosystem) โข [Integration Methods](#-choose-your-integration-method) โข [Official Packages](#-official-packages)
- [API](#api-) โข [Export Tool](#๏ธ-export-tool) โข [Available Formats](#available-formats)
- [Demo](#demo) โข [Insights](#insights) โข [Architecture](#repository-architecture)
- [Performance](#performance--benchmarks) โข [License](#-license) โข [Contributing](#contributing)
- [Platforms](#-available-on-multiple-platforms) โข [Support](#๏ธ-support-my-work)
## ๐ CSC Platform Ecosystem
Easily access all the tools and services in the Countries States Cities platform:
| Tool | Description | Link |
|-----------------|--------------------------------------------------|-------------------------------------------|
| **NPM Package** | Official JavaScript/TypeScript package | [@countrystatecity/countries](https://www.npmjs.com/package/@countrystatecity/countries) |
| **Encyclopedia** | Browse geographical data and country profiles | [countrystatecity.org](https://countrystatecity.org/) |
| **Documentation** | Complete API documentation and guides | [docs.countrystatecity.in](https://docs.countrystatecity.in/) |
| **Demo Database** | Browse the full database online | [demo.countrystatecity.in](https://demo.countrystatecity.in/) |
| **API Service** | Programmatic access to countries, states, cities | [countrystatecity.in](https://countrystatecity.in/) |
| **Export Tool** | Export data in multiple formats | [export.countrystatecity.in](https://export.countrystatecity.in/) |
| **Update Tool** | Submit and track data change requests | [manager.countrystatecity.in](https://manager.countrystatecity.in/) |
| **CLI Tool** | Command-line interface for querying CSC data | [cli.countrystatecity.in](https://cli.countrystatecity.in/) |
| **OpenAPI Spec** | Swagger/OpenAPI specification for the REST API | [GitHub](https://github.com/dr5hn/csc-swagger) |
| **API Playground** | Interactive Swagger UI to test API endpoints | [playground.countrystatecity.in](https://playground.countrystatecity.in/) |
| **Status Page** | Real-time service uptime and incidents | [status.countrystatecity.in](https://status.countrystatecity.in/) |
## ๐ Choose Your Integration Method
| Method | Setup | Best For | Offline | Cost |
|--------|-------|----------|---------|------|
| **NPM/PyPI Package** | < 1 min | JS/TS/Python apps, offline use | โ
| Free |
| **REST API** | < 5 min | Production apps, real-time data, any language | โ | Free tier + paid |
| **Export Tool** | < 2 min | Custom datasets, specific regions | โ | Free + credits |
| **Direct Download** | Immediate | SQL databases, one-time imports, legacy systems | โ
| Free |
## ๐ฆ Official Packages
### JavaScript/TypeScript (NPM)
```bash
npm install @countrystatecity/countries
```
**Features:** Zero dependencies, TypeScript support, offline-first, tree-shakeable
```javascript
import { Country, State, City } from '@countrystatecity/countries';
const usStates = State.getStatesOfCountry('US');
```
๐ [NPM Documentation](https://www.npmjs.com/package/@countrystatecity/countries) ยท ๐ [GitHub](https://github.com/dr5hn/countrystatecity-countries)
### Python (PyPI)
```bash
pip install countrystatecity
```
**Features:** Easy-to-use Python interface, works with Django/Flask, offline data access
```python
from countrystatecity import Country, State, City
us_states = State.get_states_of_country('US')
```
๐ [PyPI Package](https://pypi.org/project/countrystatecity/) ยท ๐ [GitHub](https://github.com/dr5hn/countrystatecity-pypi)
### Browser Package
Lightweight browser-native package with CDN lazy loading:
```bash
npm install @countrystatecity/countries-browser
```
**Features:** jsDelivr CDN, lazy loading, no bundled data, browser-optimized
๐ [GitHub](https://github.com/dr5hn/countrystatecity-countries-browser)
### Timezones Package
Dedicated timezone data package for JavaScript/TypeScript applications:
```bash
npm install @countrystatecity/timezones
```
๐ [GitHub Repository](https://github.com/dr5hn/countrystatecity-timezones)
## API ๐
๐ Introducing **REST API** for Countries States Cities Database.
[API](https://countrystatecity.in/)
[](https://countrystatecity.in/)
## ๐ ๏ธ Export Tool
**[Export Tool](https://export.countrystatecity.in/)** - Convert and download data in your preferred format
[](https://export.countrystatecity.in/)
**Features:** Multiple formats, flexible selection, custom filtering by region/country, bulk downloads, real-time processing
## Available Formats
### Core Formats
- **JSON** - Lightweight data interchange format
- **MYSQL** - MySQL database dumps with complete schema
- **PSQL** - PostgreSQL database exports
- **SQLITE** - Portable, self-contained database files
- **SQLSERVER** - Microsoft SQL Server compatible scripts
- **MONGODB** - NoSQL document collections + dump
- **XML** - Structured markup language format
- **YAML** - Human-readable configuration format
- **CSV** - Spreadsheet-compatible tabular data
### Geographic & AI-Optimized Formats
- **GEOJSON** - RFC 7946 standard for geographic features (Point geometry)
- **TOON** - Token-Oriented Object Notation for LLM consumption (~40% fewer tokens vs JSON) [๐ Format Spec](https://github.com/toon-format/toon)
### Optional Formats (Manual Conversion Required)
- **DuckDB** - Convert from SQLite files using provided script (not auto-generated). See [Export to DuckDB](#export-to-duckdb) for instructions.
## Distribution Files Info
| File | JSON | MYSQL | PSQL | SQLITE | SQLSERVER | MONGODB | XML | YAML | CSV | GEOJSON | TOON |
| :------------------------- | :--- | :---- | :--- | :----- | :-------- | :------ | :-- | :--- | :-- | :------ | :--- |
| Regions | โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| NA | NA |
| Subregions | โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| NA | NA |
| Countries | โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
|
| States | โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
|
| Cities | โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
| โ
|
| Country+States | โ
| NA | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| Country+Cities | โ
| NA | NA | NA | NA | NA | NA | NA | NA | NA | NA |
| Country+State+Cities/World | โ
| โ
| โ
| โ
| โ
| โ
| NA | NA | NA | NA | NA |
**Legend:** โ
= Available | NA = Not applicable for this format
## Downloads
Large export files are distributed via [GitHub Releases](https://github.com/dr5hn/countries-states-cities-database/releases) (not committed to git) to keep the repository lightweight.
**Download the latest exports:**
```bash
# Example: download cities JSON
curl -LO https://github.com/dr5hn/countries-states-cities-database/releases/latest/download/json-cities.json.gz
gunzip json-cities.json.gz
```
Small files (countries, states, regions, subregions, schema) remain in the repo and can be accessed directly via `raw.githubusercontent.com`.
## Demo
https://dr5hn.github.io/countries-states-cities-database/
## Insights
Total Regions : 6
Total Sub Regions : 22
Total Countries : 250
Total States/Regions/Municipalities : 5,299
Total Cities/Towns/Districts : 153,765
Total Timezones : 427 (100% IANA coverage)
Last Updated On: March 28, 2026
## Repository Architecture
**Two-phase build system:** JSON (version control) โ MySQL (canonical) โ All export formats
```
๐ contributions/ โ [Python Import] โ ๐๏ธ MySQL โ [PHP Export] โ ๐ฆ json/, csv/, xml/, sql/, etc.
```
**For Contributors:** Edit JSON files in `contributions/` โ Submit PR โ GitHub Actions auto-generates all exports (no local setup needed!)
**For Maintainers:** MySQL as single source of truth, dynamic schema detection, one command to regenerate all formats
**For Users:** All formats guaranteed in sync, compressed downloads on [GitHub Releases](https://github.com/dr5hn/countries-states-cities-database/releases)
## Import MongoDB
Download the MongoDB dump from [GitHub Releases](https://github.com/dr5hn/countries-states-cities-database/releases) and import:
```bash
# Download from latest release
curl -LO https://github.com/dr5hn/countries-states-cities-database/releases/latest/download/mongodb-world-mongodb-dump.tar.gz
# Extract and restore
tar -xzvf mongodb-world-mongodb-dump.tar.gz
mongorestore --host localhost:27017 --db world mongodb-dump/world
```
## Export to DuckDB
Want to export the database to DuckDB format? You can easily convert the existing SQLite files to DuckDB format using our conversion script.
### Prerequisites
First, install DuckDB Python package:
```bash
pip install duckdb
```
### Convert SQLite to DuckDB
Use the provided conversion script to convert SQLite files to DuckDB format:
```bash
# Convert the complete world database
python3 bin/scripts/export/import_duckdb.py --input sqlite/world.sqlite3 --output duckdb/world.db
# Convert individual table databases
python3 bin/scripts/export/import_duckdb.py --input sqlite/regions.sqlite3 --output duckdb/regions.db
python3 bin/scripts/export/import_duckdb.py --input sqlite/subregions.sqlite3 --output duckdb/subregions.db
python3 bin/scripts/export/import_duckdb.py --input sqlite/countries.sqlite3 --output duckdb/countries.db
python3 bin/scripts/export/import_duckdb.py --input sqlite/states.sqlite3 --output duckdb/states.db
python3 bin/scripts/export/import_duckdb.py --input sqlite/cities.sqlite3 --output duckdb/cities.db
```
The conversion script will create DuckDB database files that maintain the same structure and data as the original SQLite files, optimized for analytical workloads.
## Performance & Benchmarks
### Export Performance
| Format | Export Time | World DB Size | Compressed (.gz) |
|--------|-------------|---------------|------------------|
| **CSV** | ~1s | 40 MB | 9 MB (fastest) |
| **JSON** | ~4s | 271 MB | 18 MB |
| **MongoDB** | ~1s | 30 MB | 20 MB (dump) |
| **SQL** | ~3s | 86 MB | 22 MB |
| **SQLite** | ~45s | 89 MB | - |
| **XML** | ~9s | 91 MB | 15 MB |
| **YAML** | ~17s | 68 MB | - |
| **GeoJSON** | ~8s | 208 MB | 24 MB |
| **Toon** | ~5s | 23 MB | 20 MB |
> **๐ก Format Recommendations:**
> - **Web/Mobile Apps**: Use JSON or CSV for easy parsing
> - **Databases**: Import SQL, PSQL, or SQLite files directly
> - **GIS/Mapping**: Use GeoJSON for Leaflet, Mapbox, or PostGIS
> - **AI/LLM Projects**: Use TOON format to reduce token usage by ~40%
> - **Analytics**: SQLite for ready-to-use queries, or convert to DuckDB for advanced analytics
### API Response Times (Estimated Average)
- Countries: ~50ms | States: ~180ms | Cities by State: ~80ms | Search: ~120ms
### System Requirements
- **Contributors**: Git + text editor (no local setup needed)
- **Maintainers**: PHP 8.0+, MySQL 5.7+, Python 3.8+ (with PyMySQL), Composer, 4GB RAM, 10GB disk
- **End Users**: No requirements - just download!
## ๐ License
**[Open Database License (ODbL)](LICENSE)** - 100% free and open source!
โ
Use commercially, modify freely, share openly โข ๐ Just give credit and keep derivatives open
**Quick Attribution:**
```
Data by Countries States Cities Database
https://github.com/dr5hn/countries-states-cities-database | ODbL v1.0
```
## Contributing
### ๐ ๏ธ Easy Way: [CSC Update Tool](https://manager.countrystatecity.in/)
Use our web tool to browse, search, and submit data change requests with a streamlined review process.
[](https://manager.countrystatecity.in/)
### ๐ Manual Way: Edit JSON Files Directly
1. **Fork & clone**: `git clone --depth 1 https://github.com/YOUR_USERNAME/countries-states-cities-database.git`
2. **Edit files** in `contributions/` directory (cities, states, or countries)
3. **Omit** `id`, `created_at`, `updated_at`, `flag` fields (auto-managed)
4. **Include** required fields: `name`, `state_id`, `state_code`, `country_id`, `country_code`, `latitude`, `longitude`
5. **Submit PR** with clear description and data source
**Example city:**
```json
{
"name": "San Francisco",
"state_id": 1416,
"state_code": "CA",
"country_id": 233,
"country_code": "US",
"latitude": "37.77493",
"longitude": "-122.41942",
"timezone": "America/Los_Angeles"
}
```
๐ **Full guide**: [contributions/README.md](contributions/README.md) | [Contribution Guidelines](.github/CONTRIBUTING.md) | [Maintainer Docs](.claude/CLAUDE.md)
**Note:** Only edit JSON in `contributions/` - GitHub Actions auto-generates all export formats!
## Repo Activity

As always, thanks to our amazing contributors!
Made with [contrib.rocks](https://contrib.rocks).
## Sponsors