cauethenorio/edne-correios-loader
About
Importe os arquivos de texto do e-DNE Básico dos Correios do Brasil em seu banco de dados para habilitar a busca por CEP
Data & AnalyticsMeasured star growth
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- Update delayed
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README-en.md
Originale-DNE Correios Loader
Load all Brazilian postal codes (CEPs) into your database with a single command.
The e-DNE (National Address Directory) from Correios (Brazilian postal service) contains over 1.5 million postal codes and is freely available.
This tool automatically downloads the latest version, processes the files and loads them into any database (PostgreSQL, MySQL, SQLite, etc.), creating a unified table ready for querying.
uvx edne-correios-loader load --database-url sqlite:///dne.db
Versão em português 🇧🇷
Quick Start
With uv installed, run directly without installing:
uvx edne-correios-loader load --database-url sqlite:///dne.db
For PostgreSQL:
uvx --from 'edne-correios-loader[postgresql]' edne-correios-loader load \
--database-url postgresql://user:pass@localhost:5432/mydb
After the import, query CEPs:
$ edne-correios-loader query-cep --database-url sqlite:///dne.db 01001000
{
"cep": "01001000",
"logradouro": "Praça da Sé",
"complemento": null,
"bairro": "Sé",
"municipio": "São Paulo",
"municipio_cod_ibge": 3550308,
"uf": "SP",
"nome": null
}
What it does
- Automatically downloads the e-DNE Básico from the Correios website
- Processes the text files and loads them into a database
- Creates a unified table for querying postal codes (not included in the original DNE)
- Supports PostgreSQL, MySQL, SQLite and other databases via SQLAlchemy
- Allows customizing the names of created tables
- Updates data without service interruption (atomic transaction)
- Biweekly updates: just run the command again
Installation
If you prefer to install the package instead of using uvx:
pip install edne-correios-loader
You will also need to install the database driver that will be used:
PostgreSQL
pip install edne-correios-loader[postgresql]
If there is no pre-compiled version of psycopg2 for your operating system and Python version,
another option is to install the pg8000 driver, which is written entirely in Python.
MySQL
pip install edne-correios-loader[mysql]
SQLite
Python already provides the sqlite3 library, so no additional instructions are needed.
Others
Any database supported by SQLAlchemy can be used, such as Microsoft SQL Server and Oracle.
Usage
Command Line
$ edne-correios-loader load --help
Usage: edne-correios-loader load [OPTIONS]
Load DNE data into a database.
Options:
-s, --dne-source <path/zip-file/url>
Path or URL with the DNE file/dir to be
imported
-db, --database-url <url> Database URL where the DNE data will be
imported to [required]
--tables [unified-cep-only|cep-tables|all]
Which tables to keep in the database after
the import
--table-name <original=custom> Rename a table: --table-name original=custom
-v, --verbose Enables verbose mode.
-h, --help Show this message and exit.
Options
--dne-source(optional)Source of the e-DNE to be imported. It can be:
- A URL to a ZIP file with the e-DNE
- The local path to a ZIP file with the e-DNE
- The local path to a directory containing the e-DNE files
If this option is not provided, the latest e-DNE Basic will be automatically downloaded from the Correios website and used as the source.
--database-url(required)Database URL where the e-DNE data will be imported. The URL should follow the format
dialect+driver://username:password@host:port/database, where:dialectis the name of the database, such aspostgresql,mysql,sqlite, etc.driveris the name of the database driver, such aspsycopg2,mysqlclient,pg8000, etc. If not specified, the most popular driver is automatically used.usernameis the database user's namepasswordis the database user's passwordhostis the database server's addressportis the database server's portdatabaseis the name of the database
More information about the URL format can be found in the SQLAlchemy documentation
--tables(optional)Defines which tables will be kept in the database after the import. It can be:
unified-cep-only: Keeps only the unified CEP tablecep-tables: Keeps only the tables with CEPs, separated by typeall: Keeps all DNE tables
When not specified, the
unified-cep-onlyoption is used by default, keeping only the unified CEP table.--table-name(optional)Allows customizing a table name in the database. Can be used multiple times to rename several tables. The format is
original=custom, whereoriginalis the default table name andcustomis the desired name.Example:
--table-name cep_unificado=correios_cepUseful for integrating with projects that follow table naming conventions.
--verbose(optional)Enables verbose mode, which displays DEBUG information useful for troubleshooting during command execution.
Usage Examples
Imports the e-DNE Basic directly from the Correios website to a local SQLite database:
edne-correios-loader load --database-url sqlite:///dne.db
Same for PostgreSQL:
edne-correios-loader load --database-url postgresql://user:pass@localhost:5432/mydb
Renaming the unified table:
edne-correios-loader load \
--database-url sqlite:///dne.db \
--table-name cep_unificado=correios_cep
CEP Query
After the import, it's possible to check if the data was imported correctly by querying
CEPs in the unified table using the command edne-correios-loader query-cep.
If the unified table was renamed during import, use the --cep-table-name option:
edne-correios-loader query-cep --database-url sqlite:///dne.db --cep-table-name correios_cep 01001000
Python API
The edne-correios-loader can also be used as a Python library, through
the edne_correios_loader module. Example:
from edne_correios_loader import DneLoader, TableSetEnum
DneLoader(
# Database connection URL (required)
"postgresql://user:pass@localhost:5432/mydb",
# Path or URL to the ZIP file or directory with e-DNE files (optional)
# When omitted, the e-DNE will be automatically downloaded from the Correios website
dne_source="/path/to/dne.zip",
# Customize table names in the database (optional)
# Accepts a dict or a callable that transforms the names
table_names={"cep_unificado": "correios_cep"},
).load(
# define the tables to keep in the database after the import (optional)
# When omitted, only the unified table is kept
# Other options are TableSetEnum.CEP_TABLES and TableSetEnum.ALL
table_set=TableSetEnum.CEP_TABLES
)
The table_names parameter also accepts a callable to transform all names:
DneLoader(
"sqlite:///dne.db",
# dne_cep_unificado, dne_log_localidade, etc.
table_names=lambda name: f"dne_{name}",
).load()
After the import, CEPs can be queried in the unified table using the CepQuerier class:
from edne_correios_loader import CepQuerier
# If the table was renamed, provide the custom name
cep_querier = CepQuerier(
"postgresql://user:pass@localhost:5432/mydb",
cep_table_name="correios_cep", # optional, default: 'cep_unificado'
)
cep = cep_querier.query("01319010")
assert cep == {
"cep": "79290000",
"logradouro": None,
"complemento": None,
"bairro": None,
"municipio": "Bonito",
"municipio_cod_ibge": 5002209,
"uf": "MS",
"nome": None,
}
Updating CEPs data
Every two weeks, Correios updates the e-DNE with new postal codes. To update your database,
simply run the load command again. The latest e-DNE will be automatically downloaded from
the Correios website.
The whole process is executed inside a transaction, so other clients connected to the database will continue to have access to the old data while the update is being executed. If something goes wrong during the update, the transaction will be rolled back and the old data will be preserved.
Tests
To run the tests, you need to have Docker and uv installed. After installation:
- Clone the project:
git clone https://github.com/cauethenorio/edne-correios-loader
- Run the Docker containers with MySQL and PostgreSQL:
cd edne-correios-loader/tests
docker compose up -d
- Execute the tests using
uv:
uv run pytest tests
License
This project is distributed under the terms of the MIT license.
Explore related projects
Loading…