├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── custom.md │ └── feature_request.md └── dependabot.yml ├── .gitignore ├── Application.csproj ├── Application.sln ├── CONTRIBUTING.md ├── Dtos └── ConnectionStringDto.cs ├── Helpers └── SpectreConsoleHelper.cs ├── Program.cs ├── Properties └── PublishProfiles │ └── FolderProfile.pubxml ├── Providers ├── Interfaces │ └── IProvider.cs └── Provider.cs ├── README.md ├── SECURITY.md ├── Services ├── Interfaces │ └── IService.cs └── Service.cs ├── Validators ├── Interfaces │ └── IValidator.cs └── Validator.cs └── connectionString.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: sanamhub 2 | patreon: sanampakuwal 3 | open_collective: sanam 4 | ko_fi: sanampakuwal 5 | # tidelift: npm/@sanampakuwal%2Futils 6 | liberapay: sanam 7 | issuehunt: sanamhub 8 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.*~ 3 | project.lock.json 4 | .DS_Store 5 | *.pyc 6 | nupkg/ 7 | 8 | # Visual Studio Code 9 | .vscode 10 | 11 | # Rider 12 | .idea 13 | 14 | # User-specific files 15 | *.suo 16 | *.user 17 | *.userosscache 18 | *.sln.docstates 19 | 20 | # Build results 21 | [Dd]ebug/ 22 | [Dd]ebugPublic/ 23 | [Rr]elease/ 24 | [Rr]eleases/ 25 | x64/ 26 | x86/ 27 | build/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Oo]ut/ 32 | msbuild.log 33 | msbuild.err 34 | msbuild.wrn 35 | 36 | # Visual Studio 2015 37 | .vs/ 38 | -------------------------------------------------------------------------------- /Application.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | net9.0 5 | Application 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Application.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.5.33312.197 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application", "Application.csproj", "{166CFDF3-690A-4831-9742-6299AE585A99}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {166CFDF3-690A-4831-9742-6299AE585A99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {166CFDF3-690A-4831-9742-6299AE585A99}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {166CFDF3-690A-4831-9742-6299AE585A99}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {166CFDF3-690A-4831-9742-6299AE585A99}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {D649466E-0A4B-4B39-848C-853B295B7396} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to postgresql-to-mssql 2 | 3 | We welcome contributions to our project, postgresql-to-mssql, which is a .NET console application that handles data migration from postgresql to sql server. 4 | 5 | ### How to Contribute 6 | 7 | 1. Fork the repository and clone it to your local machine. 8 | 9 | ```bash 10 | https://github.com/sanamhub/postgresql-to-mssql.git 11 | ``` 12 | 13 | 2. Create a new branch for your changes and make sure to reference the issue number on PR (if applicable) in the branch name (e.g. "fix/issue-name"). 14 | 3. Make the necessary changes and ensure that your code adheres to the project's coding style and standards. 15 | 4. Test your changes thoroughly before submitting a pull request. 16 | 5. Submit a pull request and provide a detailed description of your changes and the reason for them. 17 | 6. Wait for your pull request to be reviewed. We'll do our best to provide feedback in a timely manner. 18 | 19 | ### Thank You 20 | 21 | Thank you for considering to contribute to `postgresql-to-mssql`. We appreciate your help in making this project better! 22 | -------------------------------------------------------------------------------- /Dtos/ConnectionStringDto.cs: -------------------------------------------------------------------------------- 1 | namespace Application.Dtos; 2 | 3 | internal class ConnectionStringDto 4 | { 5 | public string SqlServerConnectionString { get; set; } = default!; 6 | public string PostgreSqlConnectionString { get; set; } = default!; 7 | } 8 | -------------------------------------------------------------------------------- /Helpers/SpectreConsoleHelper.cs: -------------------------------------------------------------------------------- 1 | using Spectre.Console; 2 | 3 | namespace Application.Helpers; 4 | 5 | internal class SpectreConsoleHelper 6 | { 7 | public static void Log(string message) => Write($"LOG: {message}", _messageTypes[MessageTypeEnum.Log]); 8 | 9 | public static void Error(string error) => Write($"ERR: {error}", _messageTypes[MessageTypeEnum.Error]); 10 | 11 | public static void Information(string information) => Write($"INF: {information}", _messageTypes[MessageTypeEnum.Information]); 12 | 13 | public static void Success(string success) => Write($"LOG: {success}", _messageTypes[MessageTypeEnum.Success]); 14 | 15 | public static void Warning(string warning) => Write($"WRN: {warning}", _messageTypes[MessageTypeEnum.Warning]); 16 | 17 | private static void Write(string message, string color) => AnsiConsole.MarkupLine(string.Format("[{0}]{1} || {2}[/]", color, DateTime.Now, message)); 18 | 19 | public static void WriteHeader(string header, Color color) => AnsiConsole.Write(new FigletText(header).Centered().Color(color)); 20 | 21 | #region Privates 22 | 23 | private enum MessageTypeEnum 24 | { 25 | Log, 26 | Error, 27 | Information, 28 | Success, 29 | Warning 30 | } 31 | 32 | private static readonly Dictionary _messageTypes = new() 33 | { 34 | [MessageTypeEnum.Log] = "white", 35 | [MessageTypeEnum.Error] = "red", 36 | [MessageTypeEnum.Information] = "blue", 37 | [MessageTypeEnum.Success] = "green", 38 | [MessageTypeEnum.Warning] = "yellow" 39 | }; 40 | 41 | #endregion 42 | } 43 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using Application.Providers; 2 | using Application.Providers.Interfaces; 3 | using Application.Services; 4 | using Application.Services.Interfaces; 5 | using Application.Validators; 6 | using Application.Validators.Interfaces; 7 | using Microsoft.Extensions.DependencyInjection; 8 | 9 | var services = new ServiceCollection(); 10 | 11 | services.AddTransient(); 12 | services.AddTransient(); 13 | services.AddTransient(); 14 | 15 | var serviceProvider = services.BuildServiceProvider(); 16 | var service = serviceProvider.GetService(); 17 | 18 | service?.Migrate(); 19 | Console.ReadLine(); 20 | -------------------------------------------------------------------------------- /Properties/PublishProfiles/FolderProfile.pubxml: -------------------------------------------------------------------------------- 1 |  2 | 5 | 6 | 7 | Release 8 | Any CPU 9 | bin\Release\net7.0\publish\ 10 | FileSystem 11 | <_TargetId>Folder 12 | 13 | 14 | -------------------------------------------------------------------------------- /Providers/Interfaces/IProvider.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Data.SqlClient; 2 | using System.Data; 3 | 4 | namespace Application.Providers.Interfaces; 5 | 6 | internal interface IProvider 7 | { 8 | SqlConnection GetSqlServerConnection(); 9 | 10 | IDbConnection GetPostgresqlConnection(); 11 | } 12 | -------------------------------------------------------------------------------- /Providers/Provider.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Providers.Interfaces; 3 | using Microsoft.Data.SqlClient; 4 | using Npgsql; 5 | using System.Data; 6 | using System.Text.Json; 7 | 8 | namespace Application.Providers; 9 | 10 | internal class Provider : IProvider 11 | { 12 | public SqlConnection GetSqlServerConnection() => new(ConnectionString?.SqlServerConnectionString); 13 | 14 | public IDbConnection GetPostgresqlConnection() => new NpgsqlConnection(ConnectionString?.PostgreSqlConnectionString); 15 | 16 | #region Private methods 17 | 18 | private static ConnectionStringDto? ConnectionString 19 | { 20 | get 21 | { 22 | var text = File.ReadAllText("./connectionString.json"); 23 | return JsonSerializer.Deserialize(text); 24 | } 25 | } 26 | 27 | #endregion 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

⚠️ This project is still under development ⚠️

3 |
4 | 5 | 6 | 7 | # postgresql-to-mssql 8 | 9 | Migrate data from postgresql to sql server on the fly! 10 | 11 | ### Description 12 | 13 | This is a .NET console application that handles data migration (data from tables and views) from postgresql to sql server. It aims to make the process of migrating data from one database to another as seamless and easy as possible. 14 | 15 | ### Motivations 16 | 17 | As an open source enthusiast, I noticed the lack of workable open source tools for migrating data between databases, and the high cost of some paid tools. I wanted to make a contribution to the open source community by creating a tool that is both effective and accessible to all. 18 | 19 | ### Features 20 | 21 | - Migrates data from tables and views in PostgreSQL to SQL Server 22 | - Written in .NET for high performance and compatibility with a wide range of systems 23 | - Easy to use console interface 24 | - Open source and free to use 25 | 26 | ### Getting Started 27 | 28 | 1. Clone or download the repository 29 | 2. Open the solution in Visual Studio 30 | 3. Build the solution 31 | 4. Run the executable 32 | 5. Follow the prompts to configure the migration 33 | 34 | ### Contributing 35 | 36 | Please fork the repository and submit a pull request. For more [CONTRIBUTING.md](https://github.com/sanamhub/postgresql-to-mssql/blob/main/CONTRIBUTING.md) 37 | 38 | ### License 39 | 40 | This project is licensed under the [MIT License](https://github.com/sanamhub/postgresql-to-mssql/blob/main/LICENSE) 41 | 42 | ### Acknowledgements 43 | 44 | Thank you to the open source community for all of the support and resources that made this project possible. 45 | 46 | ### Support 47 | 48 | If you have any questions or issues, please open an discussion on GitHub or contact me directly. 49 | 50 | #### Happy migrating! 51 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | | Version | Supported | 6 | | ------- | ------------------ | 7 | | 1.0.x | :white_check_mark: | 8 | | 0.0.x | :x: | 9 | 10 | ## Reporting a Vulnerability 11 | 12 | Please file an issue if not exists. 13 | -------------------------------------------------------------------------------- /Services/Interfaces/IService.cs: -------------------------------------------------------------------------------- 1 | namespace Application.Services.Interfaces; 2 | 3 | internal interface IService 4 | { 5 | void Migrate(); 6 | } 7 | -------------------------------------------------------------------------------- /Services/Service.cs: -------------------------------------------------------------------------------- 1 | using Application.Helpers; 2 | using Application.Providers.Interfaces; 3 | using Application.Services.Interfaces; 4 | using Application.Validators.Interfaces; 5 | using Dapper; 6 | using Microsoft.Data.SqlClient; 7 | using Spectre.Console; 8 | using System.Data; 9 | 10 | namespace Application.Services; 11 | 12 | internal class Service : IService 13 | { 14 | private readonly IProvider _provider; 15 | private readonly IValidator _validator; 16 | 17 | public Service( 18 | IProvider provider, 19 | IValidator validator 20 | ) 21 | { 22 | _provider = provider; 23 | _validator = validator; 24 | } 25 | 26 | public void Migrate() 27 | { 28 | var errors = new List(); 29 | 30 | try 31 | { 32 | SpectreConsoleHelper.WriteHeader("pgsql to mssql", Color.Blue); 33 | 34 | _validator.ValidateProviders(); 35 | 36 | SpectreConsoleHelper.Log("Initializing..."); 37 | AnsiConsole.Status() 38 | .Spinner(Spinner.Known.Arrow3) 39 | .SpinnerStyle(Style.Parse("green")) 40 | .Start("Starting the migration...", ctx => 41 | { 42 | using var postgresConnection = _provider.GetPostgresqlConnection(); 43 | using var sqlServerConnection = _provider.GetSqlServerConnection(); 44 | 45 | postgresConnection.Open(); 46 | sqlServerConnection.Open(); 47 | 48 | ctx.Status("Fetching postgresql schemas"); 49 | ctx.Spinner(Spinner.Known.BouncingBall); 50 | var getSchemasQuery = "SELECT schema_name FROM information_schema.schemata"; 51 | var schemas = postgresConnection.Query(getSchemasQuery).ToList(); 52 | SpectreConsoleHelper.Log("Fetched schemas from postgresql..."); 53 | 54 | RemoveUnnecessarySchemas(schemas); 55 | 56 | ctx.Status("Looping through available schemas..."); 57 | foreach (var sourceSchema in schemas) 58 | { 59 | string destinationSchema = $"{sourceSchema}_new"; 60 | 61 | ctx.Status($"Creating {destinationSchema} schema in sql server..."); 62 | var createDestinationSchemaQuery = $"CREATE SCHEMA [{destinationSchema}];"; 63 | sqlServerConnection.Execute(createDestinationSchemaQuery); 64 | SpectreConsoleHelper.Log($"Created {destinationSchema} schema in sql server..."); 65 | 66 | ctx.Status($"Fetching available tables from {sourceSchema} schema..."); 67 | var getTablesQuery = $"SELECT table_name FROM information_schema.tables WHERE table_schema = '{sourceSchema}'"; 68 | var tables = postgresConnection.Query(getTablesQuery).ToList(); 69 | SpectreConsoleHelper.Log($"Fetched tables of {sourceSchema} schema from postgres"); 70 | 71 | ctx.Status($"Looping through all tables of {sourceSchema} schema..."); 72 | foreach (var table in tables) 73 | { 74 | ctx.Status($"Fetching column definition for {table} table..."); 75 | var getColumnsQuery = $"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table}' AND table_schema = '{sourceSchema}'"; 76 | var columns = postgresConnection.Query(getColumnsQuery); 77 | SpectreConsoleHelper.Log($"Fetched column definition for {table} table..."); 78 | 79 | ctx.Status($"Creating table {destinationSchema}.{table} in sql server..."); 80 | var createTableQuery = $"CREATE TABLE {destinationSchema}.{table} ("; 81 | createTableQuery += string.Join(", ", columns.Select(column => $"[{column.column_name}] {ConvertPostgreSqlToSqlServerDataType(column.data_type)}")); 82 | createTableQuery += ")"; 83 | sqlServerConnection.Execute(createTableQuery); 84 | SpectreConsoleHelper.Log($"Created table {destinationSchema}.{table} in sql server..."); 85 | 86 | IDataReader data; 87 | try 88 | { 89 | ctx.Status($"Fetching data from {sourceSchema}.{table} from postgresql..."); 90 | data = postgresConnection.ExecuteReader($"SELECT * FROM {sourceSchema}.{table}"); 91 | SpectreConsoleHelper.Log($"Fetched data from {sourceSchema}.{table} table of postgresql..."); 92 | 93 | ctx.Status("Coverting the data into proper shape before migrating to sql server..."); 94 | var dataTable = new DataTable(); 95 | dataTable.Load(data); 96 | SpectreConsoleHelper.Log("Converted data into proper shape..."); 97 | 98 | ctx.Status($"Transferring data from [blue]{sourceSchema}.{table}[/] to [green]{destinationSchema}.{table}[/]"); 99 | using var bulkCopy = new SqlBulkCopy(sqlServerConnection); 100 | bulkCopy.DestinationTableName = $"{destinationSchema}.{table}"; 101 | bulkCopy.BulkCopyTimeout = 300; 102 | bulkCopy.WriteToServer(dataTable); 103 | SpectreConsoleHelper.Success($"Successfully transferred data from {sourceSchema}.{table} to {destinationSchema}.{table}"); 104 | } 105 | catch (Exception ex) 106 | { 107 | errors.Add($"{sourceSchema}~{table}"); 108 | AnsiConsole.WriteException(ex); 109 | } 110 | } 111 | } 112 | }); 113 | SpectreConsoleHelper.WriteHeader("Success!", Color.Green); 114 | } 115 | catch (Exception ex) 116 | { 117 | AnsiConsole.WriteException(ex); 118 | } 119 | finally 120 | { 121 | if (errors.Count != 0) 122 | { 123 | var table = new Table(); 124 | table.Title("List of failed migration table/views"); 125 | 126 | table.AddColumn("SourceSchema"); 127 | table.AddColumn("SourceTable"); 128 | 129 | foreach (var error in errors) 130 | { 131 | var errorDetails = error.Split("~"); 132 | table.AddRow(errorDetails[0], errorDetails[1]); 133 | } 134 | 135 | table.Border(TableBorder.Rounded); 136 | AnsiConsole.Write(table); 137 | } 138 | } 139 | } 140 | 141 | #region Private methods 142 | 143 | private static void RemoveUnnecessarySchemas(List schemas) 144 | { 145 | schemas.Remove("information_schema"); 146 | schemas.Remove("pg_catalog"); 147 | schemas.Remove("pg_toast"); 148 | schemas.Remove("pg_temp_1"); 149 | schemas.Remove("pg_toast_temp_1"); 150 | } 151 | 152 | private static string ConvertPostgreSqlToSqlServerDataType(string postgresDataType) 153 | { 154 | var map = new Dictionary 155 | { 156 | { "bigint", "bigint" }, 157 | { "boolean", "bit" }, 158 | { "character", "char" }, 159 | { "character varying", "nvarchar(max)" }, 160 | { "date", "date" }, 161 | { "double precision", "float" }, 162 | { "integer", "int" }, 163 | { "interval", "time" }, 164 | { "numeric", "decimal" }, 165 | { "real", "real" }, 166 | { "smallint", "smallint" }, 167 | { "text", "nvarchar(max)" }, 168 | { "time", "time" }, 169 | { "timestamp", "datetime2" }, 170 | { "timestamptz", "datetimeoffset" }, 171 | { "uuid", "uniqueidentifier" }, 172 | { "bytea", "varbinary(max)" }, 173 | { "bit", "bit" }, 174 | { "bit varying", "varbinary(max)" }, 175 | { "money", "money" }, 176 | { "json", "nvarchar(max)" }, 177 | { "jsonb", "nvarchar(max)" }, 178 | { "cidr", "nvarchar(max)" }, 179 | { "inet", "nvarchar(max)" }, 180 | { "macaddr", "nvarchar(max)" }, 181 | { "tsvector", "nvarchar(max)" }, 182 | { "tsquery", "nvarchar(max)" }, 183 | { "array", "nvarchar(max)" }, 184 | { "domain", "nvarchar(max)" }, 185 | { "timestamp with time zone", "datetimeoffset" }, 186 | }; 187 | 188 | return map.TryGetValue(postgresDataType.ToLower(), out string? value) ? value.ToUpper() : "nvarchar(max)".ToUpper(); 189 | } 190 | 191 | #endregion 192 | } 193 | -------------------------------------------------------------------------------- /Validators/Interfaces/IValidator.cs: -------------------------------------------------------------------------------- 1 | namespace Application.Validators.Interfaces; 2 | 3 | internal interface IValidator 4 | { 5 | void ValidateProviders(); 6 | } 7 | -------------------------------------------------------------------------------- /Validators/Validator.cs: -------------------------------------------------------------------------------- 1 | using Application.Helpers; 2 | using Application.Providers.Interfaces; 3 | using Application.Validators.Interfaces; 4 | using System.Data; 5 | 6 | namespace Application.Validators; 7 | 8 | internal class Validator : IValidator 9 | { 10 | private readonly IProvider _provider; 11 | 12 | public Validator(IProvider provider) 13 | { 14 | _provider = provider; 15 | } 16 | 17 | public void ValidateProviders() 18 | { 19 | using (var sqlServerConnection = _provider.GetSqlServerConnection()) 20 | { 21 | if (!IsServerConnected(sqlServerConnection)) 22 | { 23 | SpectreConsoleHelper.Error("Invalid sql server connection.. ( Configure properly at connectionString.json )"); 24 | Console.ReadLine(); 25 | } 26 | } 27 | SpectreConsoleHelper.Success("Sql server connected..."); 28 | 29 | using (var postgreSqlConnection = _provider.GetPostgresqlConnection()) 30 | { 31 | if (!IsServerConnected(postgreSqlConnection)) 32 | { 33 | SpectreConsoleHelper.Error("Invalid postgresql connection.. ( Configure properly at connectionString.json )"); 34 | Console.ReadLine(); 35 | } 36 | } 37 | SpectreConsoleHelper.Success("PostgreSQL connected..."); 38 | } 39 | 40 | #region Private methods 41 | 42 | private static bool IsServerConnected(IDbConnection connection) 43 | { 44 | try 45 | { 46 | connection.Open(); 47 | return true; 48 | } 49 | catch (Exception ex) 50 | { 51 | SpectreConsoleHelper.Error(ex.Message); 52 | return false; 53 | } 54 | } 55 | 56 | #endregion 57 | } 58 | -------------------------------------------------------------------------------- /connectionString.json: -------------------------------------------------------------------------------- 1 | { 2 | "SqlServerConnectionString": "Server=localhost;Database=lms;User Id=sa;Password=1;TrustServerCertificate=True;", 3 | "PostgreSqlConnectionString": "Server=127.0.0.1;Port=5432;Database=lms;User Id=postgres;Password=postgres;" 4 | } 5 | --------------------------------------------------------------------------------