CREATE TABLE radacct (
	"RadAcctId"		BIGSERIAL PRIMARY KEY,
	"AcctSessionId"		VARCHAR(32) NOT NULL,
	"AcctUniqueId"		VARCHAR(32) NOT NULL,
	"UserName"		VARCHAR(32),
	"Realm"			VARCHAR(30),
	"NASIPAddress"		INET NOT NULL,
	"NASPortId"		INTEGER,
	"NASPortType"		VARCHAR(32),
	"AcctStartTime"		TIMESTAMP with time zone,
	"AcctStopTime"		TIMESTAMP with time zone,
	"AcctSessionTime"		INTERVAL,
	"AcctAuthentic"		VARCHAR(32),
	"ConnectInfo_start"	VARCHAR(32),
	"ConnectInfo_stop"	VARCHAR(32),
	"AcctInputOctets"		BIGINT,
	"AcctOutputOctets"	BIGINT,
	"CalledStationId"		VARCHAR(50),
	"CallingStationId"	VARCHAR(50),
	"AcctTerminateCause"	VARCHAR(32),
	"ServiceType"		VARCHAR(32),
	"FramedProtocol"		VARCHAR(32),
	"FramedIPAddress"		INET,
	"AcctStartDelay"		INTERVAL,
	"AcctStopDelay"		INTERVAL
);

CREATE INDEX radacct_active_user_idx ON radacct ("UserName") WHERE "AcctStopTime" IS NULL;
CREATE INDEX radacct_start_user_idx ON radacct ("AcctStartTime", "UserName");

CREATE TABLE radcheck (
	id		SERIAL PRIMARY KEY,
	"UserName"	VARCHAR(30) DEFAULT '' NOT NULL,
	"Attribute"	VARCHAR(30),
	op VARCHAR(2)	NOT NULL DEFAULT '==',
	"Value"		VARCHAR(40)
);
create index radcheck_UserName on radcheck ("UserName","Attribute");

CREATE TABLE radgroupcheck (
	id		SERIAL PRIMARY KEY,
	"GroupName"	VARCHAR(20) DEFAULT '' NOT NULL,
	"Attribute"	VARCHAR(40),
	op		VARCHAR(2) NOT NULL DEFAULT '==',
	"Value"		VARCHAR(40)
);
create index radgroupcheck_GroupName on radgroupcheck ("GroupName","Attribute");

CREATE TABLE radgroupreply (
	id		SERIAL PRIMARY KEY,
	"GroupName"	VARCHAR(20) DEFAULT '' NOT NULL,
	"Attribute"	VARCHAR(40),
	op		VARCHAR(2) NOT NULL DEFAULT '=',
	"Value"		VARCHAR(40)
);
create index radgroupreply_GroupName on radgroupreply ("GroupName","Attribute");

CREATE TABLE radreply (
	id		SERIAL PRIMARY KEY,
	"UserName"	VARCHAR(30) DEFAULT '' NOT NULL,
	"Attribute"	VARCHAR(30),
	op		VARCHAR(2) NOT NULL DEFAULT '=',
	"Value"		VARCHAR(40)
);
create index radreply_UserName on radreply ("UserName","Attribute");

CREATE TABLE usergroup (
	id		SERIAL PRIMARY KEY,
	"UserName"	VARCHAR(30) DEFAULT '' NOT NULL,
	"GroupName"	VARCHAR(30)
);
create index usergroup_UserName on usergroup ("UserName");

CREATE TABLE realmgroup (
	id		SERIAL PRIMARY KEY,
	"RealmName"	VARCHAR(30) DEFAULT '' NOT NULL,
	"GroupName"	VARCHAR(30)
);
create index realmgroup_RealmName on realmgroup ("RealmName");

CREATE TABLE realms (
	id		SERIAL PRIMARY KEY,
	realmname	VARCHAR(64),
	nas		VARCHAR(128),
	authport	int4,
	options		VARCHAR(128) DEFAULT ''
);

CREATE TABLE nas (
	ipaddr		INET PRIMARY KEY,
	shortname	VARCHAR(32) NOT NULL,
	secret		VARCHAR(60) NOT NULL,
	nasname		VARCHAR(128),
	type		VARCHAR(30),
	ports		int4,
	community	VARCHAR(50),
	snmp		VARCHAR(10),
	naslocation	VARCHAR(32)
);