Menu

Post image 1
Post image 2
1 / 2
0

A Simple Python Approach to MongoDB-to-PostgreSQL Migration

DEV Community·Zaylee·3 months ago
#0vqpJy9n
#dev#mongodb#collection#import#conn#photo
Reading 0:00
15s threshold

Zaylee

I recently had to migrate a large MongoDB collection to PostgreSQL for a client project. Instead of writing a complex ETL pipeline, I used a simple Python script with pandas. Here's the core logic:

python
import pandas as pd
from pymongo import MongoClient
import psycopg2

Extract from MongoDB

mongo_client = MongoClient('mongodb://localhost:27017/')
db = mongo_client['mydb']
collection = db['mycollection']
data = list(collection.find({}, {'_id': 0}))
df = pd.DataFrame(data)

Transform data

df['created_at'] = pd.to_datetime(df['created_at'])
df['price'] = df['price'].astype(float)

Load to PostgreSQL

conn = psycopg2.connect(
host='localhost',
database='mydb',
user='user',
password='password'
)
df.to_sql('mytable', conn, if_exists='replace', index=False)
conn.close()
print(f'Migrated {len(df)} records successfully!')

For larger datasets, I've been using a tool called DataBridge that handles streaming and schema mapping automatically. What's your go-to method for database migrations?

Read More