Page tree
Skip to end of metadata
Go to start of metadata


The FreeSWITCH Database Handler (freeswitch.Dbh) allows you to connect to databases from your Lua script. The advantage of this method is that it makes use of connection pooling provided by FreeSWITCH which gives a nice increase in speed when compared to creating a new TCP connection for each LuaSQL env:connect().

Here are some examples for using freeswitch.Dbh in a mod_lua script.

 Click to expand Table of Contents

Example Dialplan App

Here is a simple way to get a few variables from your db and set them as channel variables:

Assume you have a table did_users in your db with only 2 columns: did and user with a unique key over both.

XML Dialplan

dbh Dialplan Example

Note that the lua script must be run inline, so that the retrieved channel variables are available immediately.

Lua Script

Map DID to user

The script checks if a session is available. If it is, the key/value will be set as a channel variable. The result will always be printed to your console in the debug level.

Example User Directory XML

The following script is a simple way to use an SQL query to generate User Directory XML on the fly inside FreeSWITCH.

It's assumed you have a table named users in your database containing the following columns: domain, id, mailbox, number-alias, password, dial-string and user_context (all varchars).

Lua Configuration

You can enable sending XML directory lookups through Lua by adding the following lines to your lua.conf.xml:


Lua Script

Based on the mailing list: there are problems with Voicemail Inject when using Lua to serve configs (as well as personal experience), i will elaborate on this more in depth - Destreyf

MySQL Example

Note that the query in the example is formatted for MySQL; use double-quotes (") instead of backticks (`) for PostgreSQL.

db_connect.lua rewrite

Chapter 7 of the FreeSWITCH book explains a db_connect.lua script. This is a rewrite not using LuaSQL. I used a simple MS-Access database (freeswitch.accdb) to test the script.

See Also

Lua Database