Open and close database