<rss version="2.0">
  <channel>
    <title>Meet Gor - Tag: turso</title>
    <link>https://www.meetgor.com</link>
    <description>Posts tagged with turso</description>
    <language>en-us</language>
    <pubDate>Sat, 09 May 2026 05:38:52 UTC</pubDate>
    <item>
      <title>Connect LibSQL Database hosted on Turso in a Golang Application</title>
      <link>https://www.meetgor.com/posts/turso-libsql-db-golang</link>
      <description>Introduction Welcome to the new series in Golang, Let&#39;s Go with Turso. In this series, we will learn how to interact with LibSQL databases with Golang. We will</description>
      <pubDate>Mon, 30 Sep 2024 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;Welcome to the new series in Golang, Let&#39;s Go with Turso. In this series, we will learn how to interact with LibSQL databases with Golang. We will connect with a remote/local LibSQL database, create Embedded replicas, set up a local LibSQL database, and so much more as we explore and find out more features of LibSQL.&#xA;&#xA;## Connect a LibSQL database in a Golang application&#xA;&#xA;In this post, we will learn how to connect and query a LibSQL database hosted on Turso/Cloud in a Golang Application using libsql-client package. We will go from setting up golang project, installing turso-cli, creating a database on turso with the cli, connecting to the database with shell, and golang and finally, we can query the database using Golang.&#xA;&#xA;If you want to check out the YouTube video, check this out:&#xA;&#xA;[Conenct LibSQL Database hosted on Turso with Golang](https://www.youtube.com/watch?v=vBrvX0X0phw)&#xA;&#xA;&lt;iframe width=&#34;560&#34; height=&#34;315&#34; src=&#34;https://www.youtube.com/embed/vBrvX0X0phw&#34; frameborder=&#34;0&#34; allowfullscreen&gt;&lt;/iframe&gt;&#xA;&#xA;### Initializing a Golang project&#xA;&#xA;Let&#39;s start with initializing a Golang project.&#xA;&#xA;```bash&#xA;# go mod init &lt;git-provider-domain&gt;/&lt;username&gt;/&lt;project-name&gt;&#xA;# Example&#xA;&#xA;go mod init github.com/mr-destructive/lets-go-with-turso&#xA;&#xA;```&#xA;&#xA;This will initialize the project in the current directory, creating a `go.mod` file with the specification of the Golang version and the packages that we will install and use in this module.&#xA;&#xA;### Installing Turso CLI&#xA;&#xA;```bash&#xA;# Linux/Windows&#xA;curl -sSfL https://get.tur.so/install.sh | bash&#xA;curl -sSfL https://get.tur.so/install.sh | bash&#xA;&#xA;# macOS&#xA;brew install tursodatabase/tap/turso&#xA;&#xA;```&#xA;&#xA;This will install the Turso CLI. To verify that Turso CLI is installed properly, you can run the version command to check the setup.&#xA;&#xA;```&#xA;turso --version&#xA;```&#xA;&#xA;Once it is installed, we can now log in into Turso platform, simply by running the `auth signup` or `auth login` to Register or Log-in.&#xA;&#xA;```&#xA;turso auth signup&#xA;&#xA;# OR&#xA;&#xA;turso auth login&#xA;```&#xA;&#xA;This will redirect to the browser for the OAuth flow, once signed up and logged in, this will allow to interact with the Turso platform with the CLI that we downloaded.&#xA;&#xA;To make sure we are logged in as the correct user, we can run the `auth whoami` command to get the currently logged-in user.&#xA;&#xA;```&#xA;turso auth whoami&#xA;```&#xA;&#xA;This will print the username if you are logged-in. If everything seems correct, we can move ahead with the database creation step.&#xA;&#xA;### Creating a Remote LibSQL Database on Turso&#xA;&#xA;To create a LibSQL database hosted on Turso, we will use the `turso db create` command.&#xA;&#xA;```&#xA;turso db create&#xA;&#xA;# OR&#xA;&#xA;turso db create &lt;name&gt;&#xA;```&#xA;&#xA;This will create a database with the specified name, even if you don&#39;t provide the name, it will give out a random friendly two-word name to your database. It will create a database on the nearest location available from your location.&#xA;&#xA;This command will output the following:&#xA;&#xA;```&#xA;Created database &lt;db-name&gt; at group default in 1.99s.&#xA;&#xA;Start an interactive SQL shell with the following:&#xA;    turso db shell &lt;db-name&gt;&#xA;&#xA;To see information about the database, including a connection URL, run:&#xA;    turso db show &lt;db-name&gt;&#xA;&#xA;To get an authentication token for the database, run:&#xA;    turso db tokens create &lt;db-name&gt;&#xA;```&#xA;&#xA;The next step, it shows to start an interactive shell, to see information about the database, and to generate an authentication token for the database.&#xA;&#xA;We will move to the next part, which would be to create an authentication token for accessing the database from an external application.&#xA;&#xA;### Generating and Storing Authentication Token for LibSQL Database&#xA;&#xA;After we executed the `db create` command, and it created the database on the Turso cloud, there was a command hint for creating a `token` with the command `db tokens create`&#xA;&#xA;So, this command will create a JWT authentication token, that will be used to connect and read/write to the database.&#xA;&#xA;```bash&#xA;turso db tokens create &lt;db-name&gt;&#xA;&#xA;# OR&#xA;&#xA;turso db tokens create &lt;db-name&gt; --read-only&#xA;&#xA;# OR&#xA;&#xA;turso db tokens create &lt;db-name&gt; --expiration 30d&#xA;```&#xA;&#xA;We can use the simple `db tokens create &lt;db-name&gt;` to create an authentication token for the database with (read + write access). You can copy that returned token into a environment variable, or wherever your application can read that token.&#xA;&#xA;This could be stored in the environment variable as follows:&#xA;&#xA;```bash&#xA;export TURSO_AUTH_TOKEN=&#34;&lt;token&gt;&#34;&#xA;```&#xA;&#xA;To make a `read-only` token, we can use the flag `--read-only`. This will be handy, if you only have a database as a local replica, and the only purpose of the database is for querying data.&#xA;This will prevent any write operation on the database.&#xA;&#xA;We can also use the `--expiration` flag that will be used to set the duration of the token. By default the value for expiry is `never`, but that could be a little too risky if you are making a serious application. You can either set it to `7d` which will make the token expire after seven days.&#xA;&#xA;&#xA;Now, we can get the remote database URL and connect to the database. The URL could be obtained by running the command `db show &lt;db-name&gt;`&#xA;&#xA;```&#xA;turso db show &lt;db-name&gt;&#xA;```&#xA;&#xA;This will output the following:&#xA;&#xA;```bash&#xA;Name:           &lt;db-name&gt;&#xA;URL:            libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#xA;ID:             &lt;db-id&gt;   &#xA;Group:          default&#xA;Version:        0.24.22&#xA;Locations:      bom&#xA;Size:           4.1 kB&#xA;Archived:       No&#xA;Bytes Synced:   0 B&#xA;Is Schema:      No&#xA;&#xA;Database Instances:&#xA;NAME        TYPE        LOCATION&#xA;bom         primary     bom&#xA;```&#xA;&#xA;The above output shows the meta-information of the database. This also has the URL hosted on Turso. We can construct the URL using the name of the database and your username as `libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io`, you can set this in an environment variable or in the configuration wherever you can access it from the application.&#xA;&#xA;To set the URL of the database in your application, you can use the `TURSO_DB_URL` environment variable.&#xA;&#xA;```bash&#xA;export TURSO_DATABASE_URL=&#34;libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#34;&#xA;```&#xA;&#xA;So, we have a remote database URL, and the access token configured, these are the two pieces that we will need to connect, read and write to the libsql database.&#xA;&#xA;&#xA;### Installing LibSQL Client for Golang&#xA;&#xA;So, we can install the [libsql-client-go](https://pkg.go.dev/github.com/tursodatabase/libsql-client-go/libsql) package for Golang which will be used as an SDK in Golang to interact with a remote LibSQL database.&#xA;&#xA;```bash&#xA;go get github.com/tursodatabase/libsql-client-go/libsql&#xA;```&#xA;&#xA;This will install the package `libsql` into the golang module. Now, we can use this in our golang application.&#xA;&#xA;### Populating the LibSQL Database&#xA;&#xA;Moving ahead, we have a database, but it doesn&#39;t have data! So let&#39;s create some tables and insert some rows. We can use the `db shell` command to open an interactive SQL shell on a remote LibSQL database.&#xA;&#xA;```bash&#xA;turso db shell libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#xA;```&#xA;&#xA;This will be a default `sqlite3` like a shell, where we can execute SQL commands, like `.schema`, `.mode`, `.tables`, etc.&#xA;&#xA;```bash&#xA;  .dump       Render database content as SQL&#xA;  .help       List of all available commands.&#xA;  .indexes    List indexes in a table or database&#xA;  .mode       Set output mode&#xA;  .quit       Exit this program&#xA;  .read       Execute commands from a file&#xA;  .schema     Show table schemas.&#xA;  .tables     List all existing tables in the database.&#xA;```&#xA;&#xA;And definitely, we can use the normal SQL queries, to read, write and delete data from the database.&#xA;&#xA;#### Creating a Table&#xA;&#xA;First, let&#39;s create a simple table, called `posts` with columns like `id`, `title` as a `VARCHAR(100)`, `description` as a `VARCHAR(255)`, and `content` as `TEXT` which won&#39;t be `NULL`.&#xA;&#xA;```sql&#xA;CREATE TABLE posts&#xA;  (&#xA;     id          INTEGER PRIMARY KEY,&#xA;     title       VARCHAR(100),&#xA;     description VARCHAR(255),&#xA;     content     TEXT NOT NULL&#xA;  ); &#xA;```&#xA;&#xA;This will create a table `posts` on the LibSQL database, yes this will mutate the primary LibSQL database which is hosted on Turso.&#xA;&#xA;#### Inserting Rows&#xA;&#xA;Now, since we have the `posts` table, we will insert some rows into the table.&#xA;&#xA;```bash&#xA;INSERT INTO posts (title, description, content)&#xA;VALUES &#xA;    (&#39;test title&#39;, &#39;test description&#39;, &#39;test content&#39;);&#xA;```&#xA;&#xA;Now, we have some rows populated in the `posts` table. We can add more tables, and rows into the database, as usual, but this is just an example, so I&#39;ll keep it short.&#xA;&#xA;### Connecting to the LibSQL Database&#xA;&#xA;Now, we have something to query from a database, after we connect to the database via the Golang application program.&#xA;&#xA;First, we will grab two pieces to connect to the database.&#xA;&#xA;```bash&#xA;export TURSO_DATABASE_URL=&#34;libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#34;&#xA;export TURSO_AUTH_TOKEN=&#34;&lt;token&gt;&#34;&#xA;```&#xA;&#xA;Now, let&#39;s start with the Golang program code.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;    &#34;fmt&#34;&#xA;    &#34;os&#34;&#xA;)&#xA;&#xA;func main() {&#xA;    dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;    dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;    dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;}&#xA;```&#xA;&#xA;This will be the basic config to grab the database URL and the authentication token, then we can construct the `dbURL` along with `dbToken` to construct the complete dbURL which will allow to access the database.&#xA;&#xA;Moving ahead, we will import `database/sql` package that will be used to open the database connection and `github.com/tursodatabase/libsql-client-go/libsql` to connect to the remote LibSQL database.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;database/sql&#34;&#xA;    &#34;fmt&#34;&#xA;    &#34;os&#34;&#xA;&#xA;&#x9;_ &#34;github.com/tursodatabase/libsql-client-go/libsql&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;&#x9;dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;&#x9;dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;&#xA;&#x9;db, err := sql.Open(&#34;libsql&#34;, dbUrl)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to open db %s: %s&#34;, dbUrl, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#x9;defer db.Close()&#xA;&#xA;}&#xA;```&#xA;&#xA;The `sql.Open` function will open the connection to the database, this will return a `sql.DB` object. The driver selected is `libsql` and the `dbURL` is the entire URL along with the authentication token.&#xA;&#xA;&#xA;```go&#xA;type Post struct {&#xA;&#x9;Id          int&#xA;&#x9;Title       string&#xA;&#x9;Description string&#xA;&#x9;Content     string&#xA;}&#xA;&#xA;rows, err := db.Query(&#34;SELECT * FROM posts&#34;)&#xA;if err != nil {&#xA;    fmt.Fprintf(os.Stderr, &#34;failed to query: %s&#34;, err)&#xA;    os.Exit(1)&#xA;}&#xA;&#xA;for rows.Next() {&#xA;    var post Post&#xA;    if err := rows.Scan(&amp;post.Id, &amp;post.Title, &amp;post.Description, &amp;post.Content); err != nil {&#xA;        fmt.Fprintf(os.Stderr, &#34;failed to scan: %s&#34;, err)&#xA;        os.Exit(1)&#xA;    }&#xA;    fmt.Println(post)&#xA;}&#xA;defer rows.Close()&#xA;```&#xA;&#xA;Now, let&#39;s query some data from the database. We can construct the `Post` struct that will be used to grab the required fields like `Id`, `Title`, `Description`, and `Content` from the `posts` table in the database.&#xA;&#xA;Then, we will use the `db.Query` function to query the database. This function takes in a query and returns a `sql.Rows` object. We will iterate over all the `rows` returned from the database, with the `rows.Next()` that will fetch each row. Then we can `row.Scan` the row object with the appropriate and respective fields returned in the row. In this case, the `Id`, `Title`, `Description`, and the `Content` is fetched and stored into the `post` fields.&#xA;&#xA;We have fetched the rows and we can do operations on them as required, this was just a basic example. So the entire code can be found below.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;database/sql&#34;&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;os&#34;&#xA;&#xA;&#x9;_ &#34;github.com/tursodatabase/libsql-client-go/libsql&#34;&#xA;)&#xA;&#xA;type Post struct {&#xA;&#x9;Id          int&#xA;&#x9;Title       string&#xA;&#x9;Description string&#xA;&#x9;Content     string&#xA;}&#xA;&#xA;func main() {&#xA;&#x9;dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;&#x9;dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;&#x9;dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;&#xA;&#x9;db, err := sql.Open(&#34;libsql&#34;, dbUrl)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to open db %s: %s&#34;, dbUrl, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#x9;defer db.Close()&#xA;&#xA;&#x9;rows, err := db.Query(&#34;SELECT * FROM posts&#34;)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to query: %s&#34;, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#xA;&#x9;for rows.Next() {&#xA;&#x9;&#x9;var post Post&#xA;&#x9;&#x9;if err := rows.Scan(&amp;post.Id, &amp;post.Title, &amp;post.Description, &amp;post.Content); err != nil {&#xA;&#x9;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to scan: %s&#34;, err)&#xA;&#x9;&#x9;&#x9;os.Exit(1)&#xA;&#x9;&#x9;}&#xA;&#x9;&#x9;fmt.Println(post)&#xA;&#x9;}&#xA;&#x9;defer rows.Close()&#xA;&#xA;}&#xA;```&#xA;The output of the above code will result in all the rows present in the post table of the LibSQL database.&#xA;&#xA;```bash&#xA;$ go run remote.go&#xA;&#xA;{1 test title test description test content}&#xA;{2 test title test description test content}&#xA;{3 sample post libsql tutorial create db, connect, create tables, insert rows, sync}&#xA;{4 test title test description test content}&#xA;```&#xA;&#xA;I have added a few more rows to the post table, as you can see we have successfully connected, inserted, and read from the post table in the LibSQL database hosted on Turso.&#xA;&#xA;For all the code related to this article, you can check out the [Let&#39;s Go with Turso](https://github.com/mr-destructive/lets-go-with-turso) GitHub repo for all the examples and additional examples for using LibSQL with Golang.&#xA;&#xA;## Conclusion&#xA;&#xA;So, that is a wrap for this part of the series, we have explored how to connect a remote LibSQL database hosted on Turso with Golang. In the next part of the series, we will explore how to create embedded replicas on Turso&#39;s LibSQL database in Golang.&#xA;&#xA;Thank you for reading this post, If you have any questions, feedback, and suggestions, feel free to drop them in the comments.&#xA;&#xA;Happy Coding :)&#xA;</content>
      <type>posts</type>
    </item>
  </channel>
</rss>