Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Odoo 11 Development Cookbook - Second Edition

You're reading from   Odoo 11 Development Cookbook - Second Edition Over 120 unique recipes to build effective enterprise and business applications

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788471817
Length 470 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Alexandre Fayolle Alexandre Fayolle
Author Profile Icon Alexandre Fayolle
Alexandre Fayolle
Holger Brunn Holger Brunn
Author Profile Icon Holger Brunn
Holger Brunn
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Installing the Odoo Development Environment FREE CHAPTER 2. Managing Odoo Server Instances 3. Server Deployment 4. Creating Odoo Addon Modules 5. Application Models 6. Basic Server-Side Development 7. Module Data 8. Debugging and Automated Testing 9. Advanced Server-Side Development Techniques 10. Backend Views 11. Access Security 12. Internationalization 13. Automation, Workflows, Emails, and Printouts 14. Web Server Development 15. Web Client Development 16. CMS Website Development 17. Other Books You May Enjoy

Managing Odoo server databases

When working with Odoo, all of the data of your instance is stored in a PostgreSQL database. All of the standard database management tools you are used to are available, but Odoo also proposes a web interface for some common operations.

Getting ready

We assume that your work environment is set up, and you have an instance running. Do not start it using the odoo-bin start command shown in the previous recipe, as it configures the server with some options that interfere with multi database management.

How to do it...

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 that is used to protect access to the database management interface.

Accessing the database management interface

To access the database, the following steps need to be performed:

  1. Go to the login screen of your instance (if you are authenticated, log out).
  2. Click on the Manage Databases link. This will navigate to http://localhost:8069/web/database/manager (you can also point your browser directly to that URL).

Setting or changing the master password

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:

To set the master password, you need to perform the following:

  1. Click on the Set Master Password button. You will get a dialog box asking you to provide the New Master Password:
  1. Type in a non-trivial new password and click on Continue

If the master password is already set, click on the Set Master Password button at the bottom of the screen to change it. In the displayed dialog box, type the previous master password and the new one, and then click on Continue.

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 ~/.odoorc. See the next recipe for more information about the configuration file.

Creating a new database

This dialog box can be used to create a new database instance that will be handled by the current Odoo server:

  1. In the database management screen, click on the Create Database button at the bottom of the screen:
  1. Fill in the form, as follows:
    • Master Password: The master password for this instance.
    • Database Name: Input the name of the database you wish to create.
    • Password: Type the password you want to set for the admin user of the new instance.
    • Language: Select the language you wish to be installed by default in the new database in the drop-down list.
    • Country: Select the country of the main company in the drop-down list.
    • Load demonstration data: Check this box to have demonstration data. This is useful to run interactive tests or set up a demonstration for a customer, but it should not be checked for a database meant to contain production data.
If you wish to use the database to run the automated tests of the modules (refer to Chapter 8, Debugging and Automated Testing), you need to have the demonstration data, as the vast majority of the automated tests in Odoo depend on these records to run successfully.
  1. Click on the Continue button, and wait a little until the new database is initialized. You will then be redirected to the instance, connected as the administrator.
Troubleshooting: If you are redirected to a login screen, this is probably because the --db-filter option was passed to Odoo and the new database name did not match the new database name. Note that the odoo-bin 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 (refer to 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.

Duplicating a database

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 this as many times as required:

  1. In the database management screen, click on the Duplicate Database link next to the name of the database you wish to clone:

  1. Fill in the form as follows:
    • Master Password: The master password of the Odoo server
    • New Name: The name you want to give to the copy
  1. Click on the Continue button
  2. You can then click on the name of the newly created database in the database management screen to access the login screen for that database

Removing a database

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

  1. In the database management screen, click on the Delete Database link next to the name of the database you want to remove:

  1. Fill in the form; enter the Master Password, which is the master password of the Odoo server
  2. Click on the Delete button
Caution! Potential data loss!

If you selected the wrong database, and have no backup, there is no way to recover the lost data.

Backing up a database

For creating a backup, the following steps need to be performed:

In the database management screen, click on the Backup Database link next to the database you want to back up:
  1. Fill in the form:
    • Master Password: The master password of the Odoo server.
    • Backup Format: Always use zip for a production database, as it is the only real full backup format. Only use the pg_dump format for a development database where you don't really care about the file store.
  1. Click on the Backupbutton. The backup file will be downloaded to your browser.

Restoring a database backup

If you need to restore a backup, this is what you need to do:

  1. In the database management screen, click on the Restore Database button at the bottom of the screen:
  2. Fill in the form:
    • Master Password: The master password of the Odoo server.
    • File: A previously downloaded Odoo backup.
    • Database Name: Provide the name of the database in which the backup will be restored. The database must not exist on the server.
    • This database might have been moved or copied: Choose "This database was moved" if the original database was on another server, or if it has been deleted from the current server. Otherwise, choose "This database is a copy", which is a safe default.
  3. Click on the Continue button.
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.

How it works...

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 that 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 and later, this is identified as an "unset" master password, and you are 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 that 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 3, 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 that 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 that contains a copy of the documents when you configure Odoo to not keep these in the database, which is the default in 11.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 the server runs behind a reverse proxy (refer to Chapter 3, 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.

There's more...

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-bin -d testdb

The additional bonus of this command line is that you can request the 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 commands:

$ 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 that 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.
You have been reading a chapter from
Odoo 11 Development Cookbook - Second Edition - Second Edition
Published in: Jan 2018
Publisher: Packt
ISBN-13: 9781788471817
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image