> ## Documentation Index
> Fetch the complete documentation index at: https://docs.countrystatecity.in/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL Installation

> Import the published PostgreSQL dump into a new database and verify the CountryStateCity hierarchy

Use the PostgreSQL export and `psql`. This release was generated with PostgreSQL 16.15; use a compatible, supported server and client.

> [!NOTE]
>
> These instructions use [v5.2.0-export.1](https://github.com/dr5hn/countries-states-cities-database/releases/tag/v5.2.0-export.1), published 3 November 2026. Its core tables contain 250 countries, 5,327 states and 156,169 city records. API data and installed package snapshots can differ. Check the [latest release](https://github.com/dr5hn/countries-states-cities-database/releases/latest) before choosing another version.

## Download the release

Run these POSIX-shell commands in a new directory. Downloads are compressed; extract them before opening or importing.

```bash theme={null}
set -eu
mkdir csc-postgresql
cd csc-postgresql
curl -fL 'https://github.com/dr5hn/countries-states-cities-database/releases/download/v5.2.0-export.1/psql-world.sql.gz' -o world.sql.gz
gzip -d world.sql.gz
```

## Import into a new database

Use your database role in place of `postgres`. Create a new database first; do not run the import against a database containing application data.

```bash theme={null}
set -eu
createdb -U postgres csc_world
psql -U postgres -d csc_world -v ON_ERROR_STOP=1 -f world.sql
```

`ON_ERROR_STOP` makes an import error stop the client instead of allowing later statements to hide it. If an import fails, investigate it before retrying in another new database.

## Verify the import

```bash theme={null}
psql -U postgres -d csc_world -c "SELECT 'countries' AS table_name, COUNT(*) FROM public.countries UNION ALL SELECT 'states', COUNT(*) FROM public.states UNION ALL SELECT 'cities', COUNT(*) FROM public.cities;"
```

For the pinned release, expect 250 countries, 5,327 states and 156,169 city records. Use `\dt public.*` and `\d public.cities` in `psql` to inspect the imported schema.

## Query the hierarchy

Names of parent places are joined from their tables. `cities` does not store `country_name` or `state_name`; use a left join for nullable state links.

```sql theme={null}
SELECT ci.id, ci.name AS city, st.name AS state, co.name AS country
FROM cities AS ci
JOIN countries AS co ON co.id = ci.country_id
LEFT JOIN states AS st ON st.id = ci.state_id
WHERE co.iso2 = 'IN'
ORDER BY ci.id
LIMIT 10;
```

## Troubleshooting

* Confirm that you selected the export for your database engine; SQL dialects are not interchangeable.
* Check the release tag and source files when counts differ. Counts from an API or a package can describe another snapshot.
* Inspect the actual table definitions before selecting columns. See the [schema reference](https://docs.countrystatecity.in/database/schema.md).

Back up your database before replacing data or changing its schema. Keep release and source notices with distributed data; review the [ODbL licence](https://opendatacommons.org/licenses/odbl/1-0/).
Coordinates are numeric columns; the dump does not automatically add PostGIS geometry. See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/) and [PostGIS documentation](https://postgis.net/documentation/) for optional extensions, indexes, backups and account configuration.


## Related topics

- [Country State City](https://docs.countrystatecity.in/index.md)
- [Use Cases](https://docs.countrystatecity.in/tools/export-tool/use-cases.md)
- [Database Installation](https://docs.countrystatecity.in/database/installation/index.md)
- [SQL Server Installation](https://docs.countrystatecity.in/database/installation/sql-server.md)
- [Getting Started with the Export Tool](https://docs.countrystatecity.in/tools/export-tool/getting-started.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.