diff --git a/src/master/archivist/archivist.pl b/src/master/archivist/archivist.pl
index 13932f1b828450cbabbe9475dcf5cfa4330588d1..9c783d488749a26fc18d2103615f3d018a2f01af 100755
--- a/src/master/archivist/archivist.pl
+++ b/src/master/archivist/archivist.pl
@@ -471,7 +471,7 @@ if (fork == 0) {
 	my $source = connect_db 'source';
 	my $destination = connect_db 'destination';
 	my %sessions;
-	my $get_commands = $source->prepare('SELECT ssh_commands.id, start_time, end_time, login, password, remote, ts, success, command FROM ssh_commands JOIN ssh_sessions ON ssh_commands.session_id = ssh_sessions.id WHERE NOT archived');
+	my $get_commands = $source->prepare('SELECT ssh_commands.id, start_time, end_time, login, password, remote, remote_port, ts, success, command FROM ssh_commands JOIN ssh_sessions ON ssh_commands.session_id = ssh_sessions.id WHERE NOT archived');
 	my $mark_command = $source->prepare('UPDATE ssh_commands SET archived = TRUE WHERE id = ?');
 	my $store_command = $destination->prepare('INSERT INTO ssh_commands (session, timestamp, success, command) VALUES (?, ?, ?, ?)');
 	# Make sure the params are considered the correct type.
@@ -483,13 +483,13 @@ if (fork == 0) {
 	$get_session->bind_param(2, undef, { pg_type => DBD::Pg::PG_BYTEA });
 	$get_session->bind_param(3, undef, { pg_type => DBD::Pg::PG_BYTEA });
 	my $update_session = $destination->prepare('UPDATE ssh_sessions SET end_time = ? WHERE id = ?');
-	my $store_session = $destination->prepare('INSERT INTO ssh_sessions (start_time, end_time, login, password, remote) VALUES (?, ?, ?, ?, ?) RETURNING id');
+	my $store_session = $destination->prepare('INSERT INTO ssh_sessions (start_time, end_time, login, password, remote, remote_port) VALUES (?, ?, ?, ?, ?, ?) RETURNING id');
 	$store_session->bind_param(3, undef, { pg_type => DBD::Pg::PG_BYTEA });
 	$store_session->bind_param(4, undef, { pg_type => DBD::Pg::PG_BYTEA });
 	$get_commands->execute;
 	my $count_commands = 0;
 	my $count_sessions = 0;
-	while (my ($id, $start, $end, $login, $password, $remote, $time, $success, $command) = $get_commands->fetchrow_array) {
+	while (my ($id, $start, $end, $login, $password, $remote, $remote_port, $time, $success, $command) = $get_commands->fetchrow_array) {
 		my $sid = $sessions{$start}->{$login}->{$password};
 		if (not defined $sid) {
 			$get_session->execute($start, $login, $password);
@@ -497,7 +497,7 @@ if (fork == 0) {
 				$sid = $id;
 				$update_session->execute($end, $sid) if ($send ne $end);
 			} else {
-				$store_session->execute($start, $end, $login, $password, $remote);
+				$store_session->execute($start, $end, $login, $password, $remote, $remote_port);
 				($sid) = $store_session->fetchrow_array;
 				$count_sessions ++;
 			}
diff --git a/src/master/archivist/initdb b/src/master/archivist/initdb
index aec5a3171472bbef6dbc275a1de74491b81af10e..91a9f29874600c633475fb76b1eb44d2b6f7ab98 100755
--- a/src/master/archivist/initdb
+++ b/src/master/archivist/initdb
@@ -343,7 +343,9 @@ CREATE TABLE fake_passwords (
 	remote INET NOT NULL,
 	remote_port INT,
 	name BYTEA NOT NULL,
-	password BYTEA NOT NULL
+	password BYTEA NOT NULL,
+	CHECK(remote_port > 0),
+	CHECK(remote_port <= 65535)
 );
 CREATE TABLE fake_server_activity (
 	date DATE NOT NULL,
@@ -364,7 +366,10 @@ CREATE TABLE ssh_sessions (
 	login BYTEA NOT NULL,
 	password BYTEA NOT NULL,
 	remote INET,
-	UNIQUE(start_time, login, password)
+	remote_port INT,
+	UNIQUE(start_time, login, password),
+	CHECK(remote_port > 0),
+	CHECK(remote_port <= 65535)
 );
 CREATE SEQUENCE ssh_session_ids OWNED BY ssh_sessions.id;
 ALTER TABLE ssh_sessions ALTER COLUMN id SET DEFAULT NEXTVAL('ssh_session_ids');