Skip to main content

Database Architecture

The Countries States Cities database follows a hierarchical structure with clear relationships between geographical entities. The schema is designed to be normalized, efficient, and consistent across all supported database formats.

Core Tables

1. Regions Table

The top-level geographical divisions based on UN regional classifications.
integer
required
Unique identifier for the region (Primary Key)
varchar(100)
required
Official name of the region (e.g., “Europe”, “Asia”, “Africa”)
varchar(255)
Wikidata identifier for additional reference data
timestamp
Record creation timestamp
timestamp
Last modification timestamp
Sample Data:

2. Subregions Table

Subdivisions of regions providing more granular geographical classification.
integer
required
Unique identifier for the subregion (Primary Key)
varchar(100)
required
Official name of the subregion (e.g., “Western Europe”, “Southern Asia”)
integer
required
Foreign key reference to the parent region
varchar(255)
Wikidata identifier for additional reference data
timestamp
Record creation timestamp
timestamp
Last modification timestamp
Sample Data:

3. Countries Table

Complete country information with ISO codes, economic data, and geographical coordinates.
integer
required
Unique identifier for the country (Primary Key)
varchar(100)
required
Official English name of the country
char(3)
required
ISO 3166-1 alpha-3 code (e.g., “USA”, “IND”, “GBR”)
char(2)
required
ISO 3166-1 alpha-2 code (e.g., “US”, “IN”, “GB”)
char(3)
ISO 3166-1 numeric code
varchar(255)
International dialing code (e.g., “+1”, “+91”, “+44”)
varchar(255)
Name of the country’s capital city
char(3)
ISO 4217 currency code (e.g., “USD”, “EUR”, “INR”)
varchar(100)
Full name of the currency
varchar(10)
Currency symbol (e.g., ”$”, ”€”, ”₹”)
varchar(10)
Top-level domain (e.g., “.us”, “.in”, “.uk”)
varchar(255)
Native name of the country in local language/script
varchar(100)
Region name (denormalized for quick access)
varchar(100)
Subregion name (denormalized for quick access)
integer
Foreign key reference to the regions table
integer
Foreign key reference to the subregions table
varchar(100)
Demonym for country citizens (e.g., “American”, “Indian”, “British”)
decimal(10,8)
Geographical latitude coordinate of country center
decimal(11,8)
Geographical longitude coordinate of country center
varchar(10)
Unicode emoji flag representation
varchar(20)
Unicode code points for the flag emoji
integer
Country population (may be null for territories with no dedicated census)
decimal
Gross domestic product in USD billions (approximate)
decimal
Total area in square kilometres
varchar(255)
Human-readable postal code format pattern (e.g., ##### for US zip codes)
varchar(255)
Regular expression for validating postal codes for this country
json
JSON array of timezone objects for the country. Each object contains zoneName, gmtOffset, gmtOffsetName, abbreviation, and tzName.
json
JSON object of country name translations keyed by locale code (e.g., "fr": "États-Unis", "de": "Vereinigte Staaten"). 19 languages supported.
varchar(255)
Wikidata identifier for cross-referencing additional metadata
Sample Data:

4. States Table

Administrative divisions within countries (states, provinces, regions, etc.).
integer
required
Unique identifier for the state (Primary Key)
varchar(255)
required
Official name of the state/province/region
integer
required
Foreign key reference to the parent country
char(2)
required
ISO 2-letter country code for quick reference
varchar(100)
Country name (denormalized for performance)
varchar(10)
Official state/province code when available
varchar(191)
Type of administrative division (state, province, region, municipality, etc.)
varchar(10)
FIPS (Federal Information Processing Standards) code, primarily used for US states
varchar(10)
ISO 3166-2 subdivision code (e.g., US-CA, IN-MH, GB-ENG)
integer
Hierarchy level within the country. 1 = top-level region/state, 2 = department/province. Used in countries with multi-tier administration (France, Italy). Null for countries with a flat structure.
integer
Foreign key to the parent state row for level-2 subdivisions (e.g., a French department points to its region). Null for level-1 states.
varchar(255)
State name in the local/native language and script
varchar(255)
Primary IANA timezone identifier for the state (e.g., America/Los_Angeles)
json
JSON object of state name translations keyed by locale code
integer
State population estimate
varchar(255)
Wikidata identifier for cross-referencing additional metadata
decimal(10,8)
Geographical latitude coordinate of state center
decimal(11,8)
Geographical longitude coordinate of state center
Sample Data:

5. Cities Table

Cities, towns, and other populated places within states/countries.
integer
required
Unique identifier for the city (Primary Key)
varchar(255)
required
Official name of the city/town
integer
required
Foreign key reference to the parent state
varchar(10)
State code for quick reference
varchar(255)
State name (denormalized for performance)
integer
required
Foreign key reference to the country
char(2)
required
ISO 2-letter country code for quick reference
varchar(100)
Country name (denormalized for performance)
varchar(191)
Place classification inherited from GeoNames (e.g., city, town, adm2, county). See the city types reference for all 35 values and filtering guidance.
integer
Administrative hierarchy level within the state, mirroring the state-level structure where applicable
integer
Foreign key to the parent city row for nested subdivisions (e.g., a district within a city)
varchar(255)
City name in the local/native language and script
integer
City population estimate
varchar(255)
IANA timezone identifier for the city (e.g., America/Los_Angeles)
json
JSON object of city name translations keyed by locale code
decimal(10,8)
required
Geographical latitude coordinate
decimal(11,8)
required
Geographical longitude coordinate
varchar(255)
Wikidata identifier for additional reference data
Sample Data:

6. Postcodes Table

844,248 postal codes across 125 countries with type classification and source tracking. The authoritative column and API property is code — not postcode.
integer
required
Unique identifier for the postcode record (Primary Key)
varchar(20)
required
The postal / ZIP code string, as stored (e.g., 110001, GY1 1AA, 11104)
char(2)
required
ISO 3166-1 alpha-2 country code linking to the countries table
varchar(255)
State/province code, when the postcode is linked to one
varchar(255)
Name of the locality or area associated with this postcode
varchar(32)
A source-provided classification, not a fixed database enum — new values can appear as more sources are imported. The current dataset uses full (complete postcode, most countries), area (broader geographic area, e.g. Netherlands, Iran, UK), fsa (Canadian Forward Sortation Area), po_box, and street (South Africa’s distinct PO Box vs. street-delivery codes). See Postcode Types for the full breakdown.
integer
required
Foreign key reference to the parent country
integer
Foreign key reference to the linked state, when applicable
integer
Foreign key reference to the linked city, when applicable
decimal(10,8)
Geographical latitude coordinate of the postcode area
decimal(11,8)
Geographical longitude coordinate of the postcode area
varchar(64)
Data source attribution for the postcode record (e.g. openplz, wikidata, census)
varchar(255)
Wikidata identifier for cross-referencing
Sample Data:
The postcodes table is available in the self-hosted database exports and via the hosted API — see Get Postcode by Code and Get Postcodes by Country. See the postcodes reference for coverage details and self-hosted query examples.
API access levels. The hosted API tiers postcode fields the same way it tiers cities and countries: id, code, country_code, state_code, locality_name, and type are on every plan (Basic). country_id, state_id, city_id, latitude, longitude, source, and wikiDataId require Supporter+. created_at, updated_at, and flag are not exposed publicly. Self-hosted database exports are not access-tiered — all columns are present.

Indexes and Performance

Primary Indexes

All tables include clustered primary key indexes on the id field for optimal performance.

Foreign Key Indexes

Search Indexes

For full-text search capabilities, consider adding text search indexes on name fields based on your database platform’s capabilities.

Data Types by Platform

Different database platforms use slightly different data type specifications:

Relationships and Constraints

Foreign Key Constraints

Foreign key constraints are enforced in most SQL databases but may need to be handled at the application level in some NoSQL implementations.

Data Integrity Rules

  • All primary keys must be non-null and unique
  • Country names, ISO codes are required
  • State and city names are required
  • Geographic coordinates are required for cities
  • ISO2 codes must be exactly 2 characters
  • ISO3 codes must be exactly 3 characters
  • Phone codes must start with ”+”
  • Latitude must be between -90 and 90
  • Longitude must be between -180 and 180
  • States must belong to valid countries
  • Cities must belong to valid states and countries
  • Countries must belong to valid regions and subregions

Common Queries

Here are some frequently used query patterns:

Next Steps

Installation Guide

Learn how to set up the database in your preferred environment.