-
Book Overview & Buying
-
Table Of Contents
Odoo Development Cookbook
By :
When working with Odoo, all the data of your instance is stored in a PostgreSQL database. All the standard database management tools you are used to are available, but Odoo also proposes a web interface for some common operations.
We assume that your work environment is set up and you have an instance running. Do not start it using the odoo.py start command shown in the previous recipe, as it configures the server with some options which interfere with multi-database management.
The Odoo database management interface provides tools to create, duplicate, remove, back up, and restore a database. There is also a way to change the master password which is used to protect access to the database management interface.
To access the database, the following steps need to be performed:
http://localhost:8069/web/database/manager (you can also point your browser directly to that URL.).If you've set up your instance with default values, and not yet modified it as explained in the following section, the database management screen will display a warning telling you that the master password is not set, and advising you to set one, with a direct link:


The master password is in the server configuration file under the admin_password key. If the server was started without specifying a configuration file, a new one will be generated in ~/.openerp_serverrc. See the next recipe for more information about the configuration file.
This dialog box can be used to create a new database instance which will be handled by the current Odoo server:

Troubleshooting
If you are redirected to a login screen, this is probably because the option --db-filter was passed to Odoo and the new database name did not match the new database name. Note that the odoo.py start command does this silently, making only the current database available. To work around this, simply restart Odoo without the start command, as shown in the first recipe of this chapter. If you have a configuration file (see the Storing the instance configuration in a file recipe later in this chapter), then check that the db_filter option is unset or set to a value matching the new database name.
Very often you will have an existing database and you want to experiment with it to try a procedure or run a test, but without modifying the existing data. The answer is simple: duplicate the database and run the tests on the copy. Repeat as many times as required:

When you have finished your tests, you will want to clean up the duplicated databases. To do this, perform the following steps:

Caution! Potential data loss!
If you selected the wrong database, and have no backup, there is no way to recover the lost data.
For creating a backup, the following steps need to be performed:

admin by default).If you need to restore a backup, this is what you need to do:

Note: It is not possible to restore a database on top of itself. If you try to do this, you will get an error message (Database restore error: Database already exists). You need to remove the database first.
These features, apart from the Change master password screen, run postgresql administration commands on the server and report back through the web interface.
The master password is a very important piece of information which only lives in the Odoo server configuration file and is never stored in the database. There used to be a default value of admin, but using this value is a security liability as it is well known. In Odoo v9, this is identified as an "unset" master password and you get urged to change it when accessing the database administration interface. Even if it is stored in the configuration file under the admin_passwd entry, this is not the same as the password of the admin user; these are two independent passwords: the master password is set for an Odoo server process, which itself can handle multiple database instances, each of which has an independent admin user with his own password.
Security considerations: Remember that we are considering a development environment in this chapter. The Odoo database management interface is something which needs to be secured when you are working on a production server as it gives access to a lot of sensitive information, especially if the server hosts Odoo instances for several different clients. This will be covered in Chapter 16, Server Deployment.
To create a new database, Odoo uses the PostgreSQL createdb utility and calls the internal Odoo function to initialize the new database in the same way as when you start Odoo on an empty database.
To duplicate a database, Odoo uses the --template option of createdb passing the original database as an argument. This essentially duplicates the structure of the template database in the new database using internal and optimized PostgreSQL routines, which is much faster than creating a backup and restoring it (especially when using the web interface which requires downloading the backup file and uploading it again).
Backup and restore operations use the pg_dump and pg_restore utilities respectively. When using the .zip format, the backup will also include a copy of the file store which contains a copy of the documents when you configure Odoo to not keep these in the database, which is the default in 9.0. Unless you configure it otherwise, these files live in ~/.local/share/Odoo/filestore.
If the backup gets large, downloading it may fail, either because the Odoo server itself is not able to handle the large file in memory or if the server runs behind a reverse proxy (see Chapter 16, Server Deployment) because there is a limit to the size of HTTP responses set in the proxy. Conversely, for the same reasons, you will likely experience issues with the database restore operation. When you start running into these issues, it is time to invest in a more robust external backup solution.
Experienced Odoo developers generally don't use the database management interface, and perform the operations from the command line. To initialize a new database with demo data for instance, the following one-liner can be used:
$ createdb testdb && odoo.py -d testdb
The additional bonus of this command line is that you can request installation of addons while you are at it using for instance -i sale,purchase,stock (more on this in Chapter 2, Managing Odoo Server Instances).
To duplicate a database, stop the server, and run the following command:
$ createdb -T dbname newdbname $ cd ~/.local/share/Odoo/filestore # adapt if you have changed the data_dir $ cp -r dbname newdbname $ cd -
Note that in the context of development, the file store is often omitted.
The use of createdb -T only works if there are no active sessions on the database, which means you have to shut down your Odoo server before duplicating the database from the command line.
To remove an instance, run the following command:
$ dropdb dbname $ rm -rf ~/.local/share/Odoo/filestore/dbname
To create a backup (assuming the PostgreSQL server is running locally), use the following command:
$ pg_dump -Fc -f dbname.dump dbname $ tar cjf dbname.tgz dbname.dump ~/.local/share/Odoo/filestore/dbname
To restore the backup, run the following command:
$ tar xf dbname.tgz $ pg_restore -C -d dbname dbname.dump
Caution!
If your Odoo instance uses a different user to connect to the database you need to pass -U username so that the correct user is the owner of the restored database.