Skip to content

Instantly share code, notes, and snippets.

@ericdagenais
Last active January 26, 2022 21:00
Show Gist options
  • Save ericdagenais/69265926b3304314dbbc9f60a76032ba to your computer and use it in GitHub Desktop.
Save ericdagenais/69265926b3304314dbbc9f60a76032ba to your computer and use it in GitHub Desktop.
Windows: Import PgAdmin 3 Connections to Navicat and DataGrip
require 'win32/registry'
passwords = Hash.new
file = nil
begin
file = File.new("#{ENV['APPDATA']}\\postgresql\\pgpass.conf", "r")
while (line = file.gets)
c = line.split(':')
key = "#{c[0]}:#{c[1]}:#{c[2]}:#{c[3]}"
passwords[key] = c[4].strip if c.length > 4
end
rescue => err
puts "Exception: #{err}"
ensure
file.close
end
a = nil
begin
File.open('ide-scripting.js', 'w') {
|file|
file.write("var psiManager = com.intellij.util.containers.ContainerUtil.findInstance(com.intellij.database.psi.DbPsiFacade.getInstance(IDE.project).getDbManagers(), com.intellij.database.psi.DefaultDbPsiManager.class);\n");
Win32::Registry::HKEY_CURRENT_USER.open("Software\\pgAdmin III\\Servers") do |reg|
reg.each_key {
|n, v|
reg.open(n) do |sub|
description = sub['Description'].tr(" ", "_")
server = sub['Server']
port = sub['Port']
username = sub['Username']
key = "#{server}:#{port}:*:#{username}"
password = "#{passwords[key]}"
file.write("psiManager.addDataSource(new com.intellij.database.dataSource.LocalDataSource(\"#{description}\", \"org.postgresql.Driver\", \"jdbc:postgresql://#{server}:5432/postgres\", \"#{username}\", \"#{password}\"));\n")
end
}
end
}
puts "Export complete"
puts "Please use Datagrip to run the ide script located in #{File.absolute_path("./ide-scripting.js")}"
rescue => err
puts "Exception: #{err}"
end
require 'win32/registry'
passwords = Hash.new
file = nil
begin
file = File.new("#{ENV['APPDATA']}\\postgresql\\pgpass.conf", "r")
while (line = file.gets)
c = line.split(':')
key = "#{c[0]}:#{c[1]}:#{c[2]}:#{c[3]}"
passwords[key] = c[4].strip if c.length > 4
end
rescue => err
puts "Exception: #{err}"
ensure
file.close
end
a = nil
begin
File.open('connections.ncx', 'w') {
|file|
file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Connections Ver=\"1.4\">\n")
Win32::Registry::HKEY_CURRENT_USER.open("Software\\pgAdmin III\\Servers") do |reg|
reg.each_key {
|n, v|
reg.open(n) do |sub|
description = sub['Description'].tr(" ", "_")
server = sub['Server']
port = sub['Port']
username = sub['Username']
key = "#{server}:#{port}:*:#{username}"
password = "#{passwords[key]}"
file.write(" <Connection ConnectionName=\"#{description}\" ProjectUUID=\"\" ConnType=\"POSTGRESQL\" OraConnType=\"\" ServiceProvider=\"Default\" Host=\"#{server}\" Port=\"#{port}\" Database=\"postgres\" OraServiceNameType=\"\" TNS=\"\" MSSQLAuthenMode=\"\" MSSQLAuthenWindowsDomain=\"\" DatabaseFileName=\"\" UserName=\"#{username}\" Password=\"#{password}\" SavePassword=\"true\" SettingsSavePath=\"C:\\Users\\Eric\\Documents\\Navicat\\PostgreSQL\\Servers\\#{description}\" SessionLimit=\"0\" Encoding=\"\" Keepalive=\"false\" KeepaliveInterval=\"240\" MySQLCharacterSet=\"false\" Compression=\"false\" AutoConnect=\"false\" OraRole=\"\" OraOSAuthen=\"false\" SQLiteEncrypt=\"false\" SQLiteEncryptPassword=\"\" SQLiteSaveEncryptPassword=\"false\" UseAdvanced=\"false\" SSL=\"false\" SSL_Authen=\"false\" SSL_PGSSLMode=\"REQUIRE\" SSL_ClientKey=\"\" SSL_ClientCert=\"\" SSL_CACert=\"\" SSL_Clpher=\"\" SSL_PGSSLCRL=\"\" SSH=\"false\" SSH_Host=\"\" SSH_Port=\"22\" SSH_UserName=\"\" SSH_AuthenMethod=\"PASSWORD\" SSH_Password=\"\" SSH_SavePassword=\"false\" SSH_PrivateKey=\"\" SSH_Passphrase=\"\" SSH_SavePassphrase=\"false\" SSH_Compress=\"false\" HTTP=\"false\" HTTP_URL=\"\" HTTP_PA=\"\" HTTP_PA_UserName=\"\" HTTP_PA_Password=\"\" HTTP_PA_SavePassword=\"\" HTTP_EQ=\"\" HTTP_CA=\"\" HTTP_CA_ClientKey=\"\" HTTP_CA_ClientCert=\"\" HTTP_CA_CACert=\"\" HTTP_CA_Passphrase=\"\" HTTP_Proxy=\"\" HTTP_Proxy_Host=\"\" HTTP_Proxy_Port=\"\" HTTP_Proxy_UserName=\"\" HTTP_Proxy_Password=\"\" HTTP_Proxy_SavePassword=\"\"/>\n")
end
}
end
file.write("</Connections>")
}
puts "Export complete"
puts "Please use Navicat to import your connections located in #{File.absolute_path("./connections.ncx")}"
rescue => err
puts "Exception: #{err}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment