惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
爱范儿
爱范儿
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 叶小钗
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
博客园 - 司徒正美
博客园 - 【当耐特】
IT之家
IT之家

Blog — PlanetScale

Keeping a Postgres queue healthy — PlanetScale Patterns for Postgres Traffic Control — PlanetScale Graceful degradation in Postgres — PlanetScale High memory usage in Postgres is good, actually — PlanetScale Stripe Projects partnership: Provision PlanetScale Postgres and MySQL databases from the Stripe CLI — PlanetScale Enhanced tagging in Postgres Query Insights — PlanetScale Behind the scenes: How Database Traffic Control works — PlanetScale Introducing Database Traffic Control — PlanetScale Scaling Postgres connections with PgBouncer — PlanetScale Drizzle joins PlanetScale — PlanetScale Video Conferencing with Postgres — PlanetScale Faster PlanetScale Postgres connections with Cloudflare Hyperdrive — PlanetScale Introducing the PlanetScale MCP server — PlanetScale Database Transactions — PlanetScale Automating our changelog with Cursor commands — PlanetScale Postgres 18 is now available — PlanetScale Using MotherDuck with PlanetScale — PlanetScale $50 PlanetScale Metal is GA for Postgres — PlanetScale AI-Powered Postgres index suggestions — PlanetScale $5 PlanetScale is live — PlanetScale Announcing Vitess 23 — PlanetScale $50 PlanetScale Metal — PlanetScale Report on our investigation of the 2025-10-20 incident in AWS us-east-1 — PlanetScale $5 PlanetScale — PlanetScale Benchmarking Postgres 17 vs 18 — PlanetScale Larger than RAM Vector Indexes for Relational Databases — PlanetScale Partnering with Cloudflare to bring you the fastest globally distributed applications — PlanetScale Processes and Threads — PlanetScale PlanetScale for Postgres is now GA — PlanetScale Postgres High Availability with CDC — PlanetScale
Getting started with the PlanetScale CLI — PlanetScale
Brian Morrison II · 2022-07-12 · via Blog — PlanetScale

Brian Morrison II |

Most modern platforms offer an excellent UI designed to get developers up and running quickly, and PlanetScale is no exception. However, a great command-line interface (also known as a CLI) can help developers in ways such as speeding up their tasks with the platform or automating tasks using scripts and DevOps tools.

In this article, we take a look at how you can quickly get up and running with the CLI, as well as perform some of the common tasks within PlanetScale like:

  • viewing databases
  • creating databases
  • running SQL
  • and deploying schema changes

Let’s jump in!

Connecting to PlanetScale

Before you can use the CLI, make sure you have installed it per the guide on our documentation portal. You can verify that you have the CLI installed by running the following command in the terminal:

This should list the version of the PlanetScale CLI you have installed (v0.107.0 at the time of this writing).

pscale --version output

Let’s start by connecting to the PlanetScale service. In your terminal, run the following command and you should receive a confirmation code. A browser window should open as well displaying the same code:

Tip

If you don’t already have a PlanetScale account, you can also create one straight from the CLI.

pscale login example with confirmation code

confirmation code being displayed in the browser

Confirm that the codes match, then click the Confirm code button in your browser. Your terminal will display the message “Successfully logged in.” if done correctly.

Working with Databases

Let’s cover some of the common commands you’ll use when working with databases.

List your databases

To print a list of your databases, run:

As you can see, I have a single database created in my account:

list of databases in my account

For the remainder of this walk-through, we’re going to go through some real-world examples using an internal blogging platform we built with Next.js and Prisma. One of the common entities across most (if not all) blogging platforms is the Post, so let’s use this as a base to create a database and table. Here is the Post data model as defined with an ORM called Prisma:

model Post {
  id          Int          @id @default(autoincrement())
  title       String       @db.VarChar(255)
  content     String       @db.Text
  contentHtml String       @db.Text
  hidden      Boolean      @default(false)
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  author      User         @relation(fields: [authorId], references: [id], onDelete: Cascade)
  authorId    String
  likedBy     LikedPosts[]
  comments    Comment[]
  @@index([authorId])
  @@fulltext([title, content])
}

Create a database

To create a database, you can run the following command, where <DATABASE_NAME> is the name of the database you want to create:

pscale database create <DATABASE_NAME>

In this article, we’ll create and work with a database called cli-db.

pscale database create example

MySQL shell

Now, we need to drop into a MySQL shell within the database to create a table. To do this, run the following command:

Your terminal prompt should change to indicate you are now connected to and running commands in the context of the database we just created.

pscale shell output

Since this is a new database, we don’t have any tables created yet. Let’s run the following command to create a table that mirrors the Post model.

CREATE TABLE `Post` (
  `id` int NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `contentHtml` text NOT NULL,
  `hidden` tinyint(1) NOT NULL DEFAULT '0',
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` datetime(3) NOT NULL,
  `authorId` varchar(191) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `Post_authorId_idx` (`authorId`),
  FULLTEXT KEY `Post_title_content_idx` (`title`,`content`)
);

Show tables

When you hit enter, you shouldn’t get any output. You can check that the table exists with:

output from creating the Post table

Working with Branches

On top of managing your databases and tables, you can also manage your branches with the PlanetScale CLI.

List all branches

To demonstrate this, start by listing your existing branches on the database we created with:

pscale branch list cli-db

list of branches in the cli-db database

Promote a branch

In this example, there is only one branch (main), but it’s currently not flagged as a production branch, so let’s promote it to production using:

pscale branch promote cli-db main

output from promoting the main branch to production

Now that we have main set as our production branch, let’s create a branch off of main called dev. Run the following command to create that branch:

pscale branch create cli-db dev

You should get a message stating the branch was created successfully.

output from creating dev branch in cli-db

You can also check the dashboard to verify the branch exists.

the dev branch being displayed in the dashboard

Now that you have another branch to work on, let’s modify the schema of the dev branch and merge it into the main branch. Drop into a shell again with:

Since you have multiple branches, the CLI will ask which branch you want to enter. Select dev and hit enter.

entering the mysql shell with the main and dev branches

Add a new column called tag with the following SQL command in the shell.

ALTER TABLE Post ADD tag varchar(255);

You can use the DESCRIBE command to view how the table looks now.

the current schema of the Post table in the dev branch

As you can see, tag is now added to the schema in dev.

Create a deploy request

Now let’s merge the changes in the dev branch into main using by creating a new deploy request using the CLI.

pscale deploy-request create cli-db dev

This creates the deploy request for the cli-db, and we’re stating we want to merge the dev branch into the production branch.

output from creating the deploy request on the dev branch

List all deploy requests

You can show all active deploy requests with:

pscale deploy-request list cli-db

a list of the deploy requests in the cli-db

You can also see the deploy requests in the dashboard using the Deploy requests tab.

a list of the deploy branches from the dashboard

Merge a deploy request

To finish this off, let’s merge this deploy request into the main branch. In your terminal, run the following where 1 is the deploy request number shown from the previous step:

pscale deploy-request deploy cli-db 1

output from pscale deploy-request create

the deploy request displayed in the dashboard

Now you can check the schema of your main branch using the MySQL shell from the CLI. Enter into the shell with:

Select the main branch from the terminal. Describe the Post table again to verify that our changes are now active.

the schema of the Post table in the main branch after the deploy request was merged

To learn more about the PlanetScale CLI, you can use the CLI Reference page in our docs which lists all of the available commands and how to use them. You can also use pscale --help to list available commands for further help within your terminal.