import re def convert_mysql_to_pg(input_file, output_file): print(f"Converting {input_file} to {output_file}...") with open(input_file, 'r', encoding='utf-8') as f, open(output_file, 'w', encoding='utf-8') as out: out.write("-- Converted PostGIS Migration Script for PostgreSQL (Compatibility Mode)\n") out.write("SET statement_timeout = 0;\n") out.write("SET client_encoding = 'UTF8';\n") # Enable MySQL-style backslash escaping in strings out.write("SET standard_conforming_strings = off;\n") out.write("SET escape_string_warning = off;\n") out.write("CREATE EXTENSION IF NOT EXISTS postgis;\n\n") for line in f: if line.startswith('--') or not line.strip() or line.startswith('/*') or line.startswith('SET ') or line.startswith('UNSET '): continue if "COMMIT;" in line or "START TRANSACTION;" in line: continue # 1. Standard Conversion (backticks to double quotes for identifiers) # We must be careful not to replace backticks inside strings (though unlikely in these dumps) line = line.replace('`', '"') # 2. Cleanup column definitions line = re.sub(r"CHARACTER SET \w+ COLLATE \w+", "", line) if "CREATE TABLE" in line: line = line.replace("int(11)", "integer") line = line.replace("int NOT NULL", "integer NOT NULL") line = line.replace("bigint(20)", "bigint") line = line.replace("double NOT NULL", "double precision NOT NULL") line = line.replace("POINT NOT NULL SRID 4326", "geometry(Point, 4326)") line = line.replace("point NOT NULL", "geometry(Point, 4326)") line = re.sub(r"ENGINE=InnoDB.*?;", ";", line) # 3. Handle Hex data (NULL works best, triggers will fix it) line = re.sub(r"0x[0-9a-fA-F]+", "NULL", line) # Since standard_conforming_strings = off, we DON'T need to replace \' with '' # Postgres will understand \' as a literal quote. # We just need to ensure backslashes themselves are handled if they were double-escaped. # Actually, with standard_conforming_strings = off, MySQL's escaping should work AS IS. # But wait, my previous script changed \' to ''. Let's revert that and just clean Definitions. # 4. Fix AUTO_INCREMENT if "AUTO_INCREMENT" in line: line = re.sub(r"integer NOT NULL AUTO_INCREMENT", "SERIAL PRIMARY KEY", line) out.write(line) print("Success!") if __name__ == "__main__": convert_mysql_to_pg('geocodeDB (1).sql', 'converted_data.sql')