A .NET 8 console application that migrates schema and data from PostgreSQL to Microsoft SQL Server. The tool automatically handles case conversion from snake_case (PostgreSQL) to PascalCase (SQL Server) and provides comprehensive logging throughout the migration process.
- Schema Migration: Automatically extracts and recreates table schemas
- Data Migration: Migrates all data in configurable batches
- Case Conversion: Converts snake_case (PostgreSQL) to PascalCase (SQL Server)
- Index & Foreign Key Support: Recreates indexes and foreign key constraints
- Comprehensive Logging: Detailed logging of all migration steps
- Configurable: Easy configuration via appsettings.json
- Skip Tables: Ability to skip specific tables (e.g., __EFMigrations)
- Batch Processing: Configurable batch size for large datasets
- .NET 8.0 SDK or Runtime
- PostgreSQL database (source)
- Microsoft SQL Server database (target)
- Network access to both databases
- Clone or download this repository
- Navigate to the project directory
- Restore NuGet packages:
dotnet restore
Edit the appsettings.json file to configure your database connections and migration settings:
{
"ConnectionStrings": {
"PostgreSQL": "Host=localhost;Database=your_postgres_db;Username=your_username;Password=your_password",
"SqlServer": "Server=localhost;Database=your_mssql_db;Trusted_Connection=true;TrustServerCertificate=true"
},
"MigrationSettings": {
"BatchSize": 1000,
"SkipTables": ["__EFMigrations"],
"SkipStoredProcedures": true,
"SkipViews": true,
"EnableLogging": true
}
}PostgreSQL:
Host=localhost;Database=mydb;Username=postgres;Password=mypassword;Port=5432
SQL Server:
Server=localhost;Database=mydb;Trusted_Connection=true;TrustServerCertificate=true
or with SQL authentication:
Server=localhost;Database=mydb;User Id=sa;Password=mypassword;TrustServerCertificate=true
- Configure your connection strings in
appsettings.json - Run the migration:
dotnet run
The application will:
- Extract schema information from PostgreSQL
- Create tables in SQL Server
- Migrate data in batches
- Create indexes and foreign key constraints
- Provide detailed logging throughout the process
The migration follows these steps:
- Schema Extraction: Reads table definitions, columns, primary keys, foreign keys, and indexes from PostgreSQL
- Table Creation: Creates tables in SQL Server with proper data type mapping
- Data Migration: Migrates data in configurable batches to handle large datasets efficiently
- Constraint Creation: Creates indexes and foreign key constraints after data migration
The tool automatically maps PostgreSQL data types to SQL Server equivalents:
| PostgreSQL | SQL Server |
|---|---|
| integer | int |
| bigint | bigint |
| smallint | smallint |
| serial | int (IDENTITY) |
| decimal/numeric | decimal |
| real | real |
| double precision | float |
| character varying/varchar | nvarchar |
| text | nvarchar(max) |
| boolean | bit |
| date | date |
| timestamp | datetime2 |
| timestamp with time zone | datetimeoffset |
| uuid | uniqueidentifier |
| json/jsonb | nvarchar(max) |
- BatchSize: Number of rows to process in each batch (default: 1000)
- SkipTables: Array of table names to skip during migration
- SkipDataMigrationTables: Array of table names to skip during data migration only
- SkipStoredProcedures: Skip stored procedure migration (currently not implemented)
- SkipViews: Skip view migration (currently not implemented)
- EnableLogging: Enable detailed logging (default: true)
- ScriptGenerationMethod: Method for generating data scripts ("Optimized", "HighPerformance", "BulkInsert")
- UseBulkInsert: Use bulk insert for data migration (default: false)
- DisableForeignKeyConstraints: Disable foreign key constraints during data migration (default: true)
- ValidateScriptsForDuplicates: Validate generated scripts for duplicate rows (default: true)
- EnableIdentityInsert: Enable IDENTITY_INSERT for tables with identity columns (default: true)
The tool automatically detects identity columns (PostgreSQL sequences) and handles them appropriately:
- Schema Migration: Identity columns are created with
IDENTITY(1,1)in SQL Server - Data Migration: When
EnableIdentityInsertis enabled (default), the tool automatically addsSET IDENTITY_INSERT ON/OFFstatements to data migration scripts - Configuration: You can disable identity insert handling by setting
EnableIdentityInserttofalseinappsettings.json
This ensures that identity column values are preserved during migration, preventing "IDENTITY_INSERT is not ON" errors.
The application provides comprehensive logging including:
- Migration progress
- Table creation status
- Data migration progress
- Error details
- Performance metrics
The application includes robust error handling:
- Connection validation
- Schema validation
- Data type compatibility checks
- Detailed error messages with context
- Stored procedures and views are not currently migrated
- Some advanced PostgreSQL features may not have direct SQL Server equivalents
- Large object (LOB) data types may require special handling
- Custom PostgreSQL extensions are not supported
- Connection Errors: Verify connection strings and network connectivity
- Permission Errors: Ensure database user has appropriate permissions
- Data Type Errors: Check for unsupported data types in your schema
- Memory Issues: Reduce batch size for very large tables
To enable more detailed logging, modify the logging level in Program.cs:
builder.SetMinimumLevel(LogLevel.Debug);Feel free to submit issues and enhancement requests!
This project is provided as-is for educational and development purposes.