use postgres sequences and stored proc

postgres sequence and the base64 encoding in a stored proc so only one
query