back to home

dr5hn / countries-states-cities-database

๐ŸŒ Discover our global repository of countries, states, and cities! ๐Ÿ™๏ธ Get comprehensive data in JSON, SQL, PSQL, SQLSERVER, MONGODB, SQLITE, XML, YAML, and CSV formats. Access ISO2, ISO3 codes, country code, capital, native language, timezones (for countries), and more. #countries #states #cities

9,291 stars
2,964 forks
34 issues
PythonPHPTypeScript

AI Architecture Analysis

This repository is indexed by RepoMind. By analyzing dr5hn/countries-states-cities-database in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.

Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.

Embed this Badge

Showcase RepoMind's analysis directly in your repository's README.

[![Analyzed by RepoMind](https://img.shields.io/badge/Analyzed%20by-RepoMind-4F46E5?style=for-the-badge)](https://repomind-ai.vercel.app/repo/dr5hn/countries-states-cities-database)
Preview:Analyzed by RepoMind

Repository Summary (README)

Preview

banner

๐ŸŒ Countries States Cities Database

License: ODbL-1.0 GitHub stars GitHub forks release size

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 (shallow clone ~1.5GB vs ~9GB full history)

Table of Contents

๐ŸŒ CSC Platform Ecosystem

Easily access all the tools and services in the Countries States Cities platform:

ToolDescriptionLink
NPM PackageOfficial JavaScript/TypeScript package@countrystatecity/countries
EncyclopediaBrowse geographical data and country profilescountrystatecity.org
DocumentationComplete API documentation and guidesdocs.countrystatecity.in
Demo DatabaseBrowse the full database onlinedemo.countrystatecity.in
API ServiceProgrammatic access to countries, states, citiescountrystatecity.in
Export ToolExport data in multiple formatsexport.countrystatecity.in
Update ToolSubmit and track data change requestsmanager.countrystatecity.in
Status PageReal-time service uptime and incidentsstatus.countrystatecity.in

๐Ÿ”„ Choose Your Integration Method

MethodSetupBest ForOfflineCost
NPM/PyPI Package< 1 minJS/TS/Python apps, offline useโœ…Free
REST API< 5 minProduction apps, real-time data, any languageโŒFree tier + paid
Export Tool< 2 minCustom datasets, specific regionsโŒFree + credits
Direct DownloadImmediateSQL databases, one-time imports, legacy systemsโœ…Free

๐Ÿ“ฆ Official Packages

JavaScript/TypeScript (NPM)

npm install @countrystatecity/countries

Features: Zero dependencies, TypeScript support, offline-first, tree-shakeable

import { Country, State, City } from '@countrystatecity/countries';
const usStates = State.getStatesOfCountry('US');

๐Ÿ“– NPM Documentation ยท ๐Ÿ“‚ GitHub

Python (PyPI)

pip install countrystatecity

Features: Easy-to-use Python interface, works with Django/Flask, offline data access

from countrystatecity import Country, State, City
us_states = State.get_states_of_country('US')

๐Ÿ“– PyPI Package ยท ๐Ÿ“‚ GitHub

Timezones Package

Dedicated timezone data package for JavaScript/TypeScript applications:

npm install @countrystatecity/timezones

๐Ÿ“‚ GitHub Repository

API ๐Ÿš€

๐ŸŽ‰ Introducing REST API for Countries States Cities Database.

API

banner

๐Ÿ› ๏ธ Export Tool

Export Tool - Convert and download data in your preferred format

banner

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

Optional Formats (Manual Conversion Required)

  • DuckDB - Convert from SQLite files using provided script (not auto-generated). See Export to DuckDB for instructions.

Distribution Files Info

FileJSONMYSQLPSQLSQLITESQLSERVERMONGODBXMLYAMLCSVGEOJSONTOON
Regionsโœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…NANA
Subregionsโœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…NANA
Countriesโœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…
Statesโœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…
Citiesโœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…
Country+Statesโœ…NANANANANANANANANANA
Country+Citiesโœ…NANANANANANANANANANA
Country+State+Cities/Worldโœ…โœ…โœ…โœ…โœ…โœ…NANANANANA

Legend: โœ… = Available | NA = Not applicable for this format

Demo

https://dr5hn.github.io/countries-states-cities-database/

Insights

Total Regions : 6 <br> Total Sub Regions : 22 <br> Total Countries : 250 <br> Total States/Regions/Municipalities : 5,299 <br> Total Cities/Towns/Districts : 153,765 <br> Total Timezones : 427 (100% IANA coverage) <br>

Last Updated On: December 13, 2025

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 available (.gz)

Import MongoDB

How to import MongoDB database?

# First extract the tar.gz file
tar -xzvf world-mongodb-dump.tar.gz

# Then restore the MongoDB dump
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:

pip install duckdb

Convert SQLite to DuckDB

Use the provided conversion script to convert SQLite files to DuckDB format:

# 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

FormatExport TimeWorld DB SizeCompressed (.gz)
CSV~1s40 MB9 MB (fastest)
JSON~4s271 MB18 MB
MongoDB~1s30 MB20 MB (dump)
SQL~3s86 MB22 MB
SQLite~45s89 MB-
XML~9s91 MB15 MB
YAML~17s68 MB-
GeoJSON~8s208 MB24 MB
Toon~5s23 MB20 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) - 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

Use our web tool to browse, search, and submit data change requests with a streamlined review process.

banner

๐Ÿ“ 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:

{
  "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 | Contribution Guidelines | Maintainer Docs

Note: Only edit JSON in contributions/ - GitHub Actions auto-generates all export formats!

Repo Activity

Repo Activity

As always, thanks to our amazing contributors!

<a href="https://github.com/dr5hn/countries-states-cities-database/graphs/contributors"> <img src="https://contrib.rocks/image?repo=dr5hn/countries-states-cities-database&anon=1" /> </a>

Made with contrib.rocks.

Sponsors

<p align="center"> <a href="https://cdn.jsdelivr.net/gh/dr5hn/static/sponsors.svg"> <img src='https://cdn.jsdelivr.net/gh/dr5hn/static/sponsors.svg'/> </a> </p>

Make the world more Greener ๐ŸŒด

Contribute towards better earth buy the world a tree

๐ŸŒ Available On Multiple Platforms

Find and use this dataset across the web - choose the platform that fits your workflow:

PlatformBest ForAccess
๐Ÿ“š EncyclopediaBrowse country profiles, geographical dataVisit Encyclopedia
๐Ÿ“Š Kaggle DatasetData science, ML projects, notebooksDownload on Kaggle
๐Ÿ—ƒ๏ธ Data.worldData collaboration, business analyticsView on Data.world
๐Ÿ“ฆ NPM RegistryJavaScript/TypeScript developersnpm install @countrystatecity/countries
๐Ÿ™ GitHubContributors, raw files, issue trackingView Repository
๐ŸŒ API ServiceProduction apps, real-time accessGet API Key
๐Ÿ› ๏ธ Export ToolCustom exports, specific formatsLaunch Tool
๐Ÿ“Š Status PageService uptime monitoring, incidentsCheck Status

Follow me at

<a href="https://github.com/dr5hn/"><img alt="Github @dr5hn" src="https://img.shields.io/static/v1?logo=github&message=Github&color=black&style=flat-square&label=" /></a> <a href="https://twitter.com/dr5hn/"><img alt="Twitter @dr5hn" src="https://img.shields.io/static/v1?logo=twitter&message=Twitter&color=black&style=flat-square&label=" /></a> <a href="https://www.linkedin.com/in/dr5hn/"><img alt="LinkedIn @dr5hn" src="https://img.shields.io/static/v1?logo=linkedin&message=LinkedIn&color=black&style=flat-square&label=&link=https://twitter.com/dr5hn" /></a>

๐Ÿ™‹โ€โ™‚๏ธ Support My Work

Github Sponsorship

ko-fi

Suggestions / Feedbacks

Suggestions & Feedbacks are Most Welcome
gadadarshan[at]gmail[dot]com

Disclaimer

While we strive for accuracy, this community-maintained database may contain errors or not reflect latest geopolitical changes. Users should verify critical data with official sources and use at their own discretion. Licensed under ODbL - see LICENSE for details.

Report issues: GitHub Issues


That's all Folks. Enjoy! ๐ŸŒ