Problem PG::ConnectionBad when merging two forums despite several previous successes

I got the same error. Seems like the port is not being loaded in the DB configs here: discourse/script/bulk_import/discourse_merger.rb at main · discourse/discourse · GitHub

If you run that from a rails console:

[1] pry(main)> ActiveRecord::Base.connection_db_config.configuration_hash
=> {:prepared_statements=>false,
 :adapter=>"postgresql",
 :database=>"discourse_development",
 :min_messages=>"warning",
 :pool=>5,
 :timeout=>5000,
 :checkout_timeout=>5,
 :advisory_locks=>false,
 :host_names=>["localhost"],
 :variables=>{}}

The port is not in the config, but the code assumes it is: discourse/script/bulk_import/discourse_merger.rb at main · discourse/discourse · GitHub

While this is fixed in core, you can put the port manually, so instead of

 @raw_connection = PG.connect(dbname: local_db[:database], host: 'localhost', port: local_db[:port], user: 'postgres', password: db_password)

use:

@raw_connection = PG.connect(dbname: local_db[:database], host: 'localhost', port: 5432, user: 'postgres', password: db_password)