Updated
—
4 min read

Dealing with legacy databases like Microsoft Access can be a huge pain.
While paid database migration services exist, I wanted an easy way to convert my .accdb files to PostgreSQL, without owning a Microsoft Access license or needing a Office 360 subscription.
This short tutorial will show you how to convert Microsoft Access database files (.MDB or .ACCDB) to PostgreSQL (`.sql).
I highly recommend Jakob Egger's MDB/ACCDB Viewer app if you work with Microsoft Access databases on a Mac (not affiliated, I just like the software)
Dealing with legacy Microsoft Access databases can be a pain, and there are many reasons why you may want to convert Access to SQL:
Regardless of your reasoning, here's a simple way to programmatically export Microsoft Access to PostgreSQL (or MySQL, Oracle, SQLite).
Let's dive in.
Before we start, here's the prerequisites for this tutorial.
psql)First, we'll need to install mdbtools to extract data from Microsoft Access files.
mdbtools is a fantastic library for accessing MDB and ACCDB files programmatically.
To install it on Mac:
brew install mdbtools
To install on Linux:
# Ubuntu/Debian sudo apt-get install mdbtools # Fedora sudo dnf install mdbtools
Native Windows binaries of mdbtools are not available, but you can explore these workarounds to install mdbtools on Windows: 1, 2
Then confirm the installation:
mdb-tables --version
We'll be using the mdb-schema tool to extract the table schemas from Access, which we'll then use to pass to another database (PostgreSQL in this case) to create a replica of the original table format.
The supported backend values are access, sybase, oracle, postgres, mysql and sqlite
Next we'll setup a simple python script to run mdbtools and migrate our .accdb to our PostgreSQL server.
import subprocess def accdb_to_postgres(accdb_file, db_name, host, user, password): # get a list of tables tables = ( subprocess.check_output(["mdb-tables", "-1", accdb_file]) .decode() .strip() .split("\n") ) # construct postgresql connection string pg_conn = f"postgresql://{user}:{password}@{host}/{db_name}" # iterate through tables for table in tables: print(f"Processing table {table}...") # get schema (need to convert Access types to PostgreSQL types) # supported backends: `access`, `sybase`, `oracle`, `postgres`, `mysql` and `sqlite` schema = subprocess.check_output(["mdb-schema", f"--table={table}", accdb_file, "postgres"]).decode() # temporarily store schema with open("temp.sql", "w") as f: f.write(schema) # create table in postgres subprocess.run(["psql", pg_conn, "-f", "temp.sql"]) # export data and import to postgres data = subprocess.check_output(["mdb-export", accdb_file, table]).decode() process = subprocess.Popen( ["psql", pg_conn, "-c", f"\copy {table} FROM STDIN WITH CSV HEADER"], stdin=subprocess.PIPE, ) process.communicate(data.encode())
This function uses mdb-tables to extract a list of Access tables, connects to PostgreSQL (or whatever database you want) and iterates through the tables.
For each table, the function copies and converts the Access schema, creates the table in SQL, and exports the data before importing it to your target database.
And that's it! Now you can run the function:
accdb_to_postgres( "/path/to/database.accdb", "db_name" "127.0.0.1", "postgres", "password", )
You should see output that looks like:
python3 ./main.py Processing table users... SET CREATE TABLE ALTER TABLE COPY 2172 Processing table sessions...
And so on. While this process works seamlessly for many cases, there are some limitations you should be aware of.
mdbtoolsI hope this tutorial helps you convert .accdb to PostgreSQL (.sql) programmatically.
If you have any questions, leave them down below.
Join my newsletter for lessons, experiments, and failures in bootstrapping online businesses.
Sign up if you're curious. I’ll only email you if it's actually good.

Meet the Author
Ryan Chiang
Hello, I'm Ryan. I build things and write about them. This is my blog of my learnings, tutorials, and whatever else I feel like writing about.
What I'm currently building →.