WARNING: Updating the PostgreSQL database via SQL is considered a very advanced technique and should never be done unless you have a complete understanding of the Odoo schema and business logic. You should backup your database and test this on a backup and only do this on a production system if you are 100% certain it works the way you want.
AP and AR accounts for partners are stored in the ir_property table. Set one partner with the accounts you want and take a look at this table in SQL to get familiar with the structure and how these properties are represented.
Query the table this way:
SELECT name, type, company_id, fields_id, value_reference, res_id FROM ir_property WHERE name = 'property_account_receivable' or name = 'property_account_payable';
An example output would be this:
name, type, company_ud, fields_id, value_reference, res_id
property_account_payable, many2one, 1, 1960, "account.account,64, "res.partner,17"
property_account_receivable, many2one, 1, 1957, "account.account,59", "res.partner,17"
Looking at res_id - this tells you that the property is being attached to record 17 of the res_partner model.
Looking at fields_id - this tells you that the property is being attached to field 1960 (payable) or 1957 (receivable) of the res_partner model.
Looking at value_reference - this tells you that the property is a link to an account and that ID 64 represents the payable account and ID 59 represents the receivable account.
Once you verify how this looks on your database, for each partner you run the following two SQL statements:
insert into ir_property (create_uid,create_date, write_date,write_uid, name,type,company_id,fields_id,value_reference,res_id) VALUES (1,current_date,current_date,1,'property_account_receivable','many2one',1,1960,'account.account,64','res.partner,db_id_of_partner');
insert into ir_property (create_uid,create_date, write_date,write_uid, name,type,company_id,fields_id,value_reference,res_id) VALUES (1,current_date,current_date,1,'property_account_payable','many2one',1,1957,'account.account,59','res.partner,db_id_of_partner');
TIP: If you are rusty on your SQL, you can export all of the database id's into a CSV file, prepend the relevant SQL, replace the linefeed character with the end quote and a semicolon, save the result as a *.sql file and import into the database (you can do this in a text editor). I did this because it was easier for me than working out how to loop through every partner.