copy (select extension,v_extensions.password,number_alias,accountcode,call_timeout,effective_caller_id_name,effective_caller_id_number,outbound_caller_id_name,outbound_caller_id_number,limit_max,toll_allow,call_timeout,call_group,user_record,hold_music,description, device_mac_address, device_label, device_vendor, device_model, device_template, device_description, voicemail_enabled, voicemail_mail_to, voicemail_password, voicemail_attach_file, voicemail_description from v_extensions LEFT JOIN v_device_lines on extension=user_id LEFT JOIN v_devices ON v_device_lines.device_uuid=v_devices.device_uuid LEFT JOIN v_voicemails on cast(extension as integer)=voicemail_id) TO '/tmp/extensions.csv' DELIMITER ',' CSV HEADER;