January 28, 2026Randolph Waelchi PhD5 min read

How to Generate Mock Data for PostgreSQL Efficiently

Discover effective methods to generate mock data for PostgreSQL, enhancing your database testing and development processes.

How to Generate Mock Data for PostgreSQL Efficiently


Creating robust and reliable applications often requires extensive testing, and a crucial element in this testing is using realistic datasets. When working with PostgreSQL, generating mock data can significantly enhance your development and testing processes. This guide will walk you through various tools and techniques to efficiently generate mock data for PostgreSQL.

Why Do You Need Mock Data?

Mock data is essential for:

  • Testing: Ensures that your application performs well with data similar to production scenarios.
  • Development: Allows developers to populate databases quickly, facilitating easier debugging and feature testing.
  • Performance Tuning: Helps in stress testing and optimizing database queries.

Tools for Generating Mock Data

1. Faker

Faker is a popular library in Python that generates fake data for you. It’s useful for generating names, emails, addresses, and more.

Installation

pip install Faker

Usage Example

from faker import Faker
import psycopg2

fake = Faker()

# Connect to your postgres DB
conn = psycopg2.connect("dbname=test user=postgres password=secret")

# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a command: this creates a new table
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    address VARCHAR(255)
)""")

# Insert mock data
for _ in range(100):
    cur.execute(
        "INSERT INTO users (name, email, address) VALUES (%s, %s, %s)",
        (fake.name(), fake.email(), fake.address())
    )

# Commit the transaction
conn.commit()

# Close communication with the database
cur.close()
conn.close()

2. Mockaroo

Mockaroo is an online tool that allows you to design your data schema and export it in various formats such as CSV, JSON, and SQL.

Steps to Use Mockaroo

  1. Go to Mockaroo.
  2. Define your schema fields.
  3. Set the number of rows and data type for each field.
  4. Export the data in SQL format and import it into your PostgreSQL database.

3. Drawline.app

For a more visual approach, use drawline.app, an AI-powered diagramming and development tool. It helps developers visualize the data model and generate mock data seamlessly, accelerating the development process.

Creating Custom Scripts

If your mock data requirements are unique or complex, consider writing custom scripts. Python's random library can be used in conjunction with SQL to generate custom datasets.

Example Script

import random
import psycopg2

names = ["Alice", "Bob", "Charlie", "David"]
emails = ["alice@example.com", "bob@example.com", "charlie@example.com", "david@example.com"]

# Connect to your postgres DB
conn = psycopg2.connect("dbname=test user=postgres password=secret")
cur = conn.cursor()

for _ in range(50):
    name = random.choice(names)
    email = random.choice(emails)
    cur.execute(
        "INSERT INTO users (name, email) VALUES (%s, %s)",
        (name, email)
    )

conn.commit()
cur.close()
conn.close()

Conclusion

Generating mock data for PostgreSQL is an invaluable practice for developers seeking to enhance their application's reliability and performance. By using tools like Faker, Mockaroo, and Drawline.app, you can quickly populate your database with realistic data, streamlining your testing and development processes.


Continue Reading

Explore more articles from our engineering team.

Back to Blog