How to connect to Couchbase 5 with Python 3

  • To connect to Couchbase 5, firstly please ensure that you have setup user and password for access (unless you are Administrator). Please note that using full right administrator account to connect via any program impose extremely high security risk!By now you should have already installed driver on your client system and Couchbase python package.
  • Here is a tutorial on how to install Couchbase driver. It is very important to have driver installed before installing python Couchbase library! Once you have drivers¬† install python library by issuing the following command in your virtual environment:
    pip3 install couchbase

    Now, you are fully geared up to connect to Couchbase server from you python program.

  • To conne Statusct to Couchbase, just put the below to your program: Status Status
    from couchbase.cluster import Cluster
    from couchbase.cluster import PasswordAuthenticator
    cluster = Cluster('couchbase://localhost')
    authenticator = PasswordAuthenticator('username', 'password')
    cluster.authenticate(authenticator)
    cb = cluster.open_bucket('bucket-name')
  • From this point cb object holds your connection. You may insert data (or read, or do whatever you intended and have permissions for). For example,¬† you can insert data like this:
    cb.upsert('u:my_main_value', {'my_structure_key':'my_structure_value'});
  • Done!