Publish and receive messages in Pub/Sub by using the gcloud CLI
This page shows you how to do the following operations in Pub/Sub using the Google Cloud CLI:
- Create a topic and subscription.
- Publish messages to the topic.
- Receive messages from the subscription.
To follow step-by-step guidance for this task directly in the Google Cloud console, clickGuide me:
Before you begin
Complete the following steps before running your pipeline.
Set up your project
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Set up a Google Cloud console project.
Click to:
- Create or select a project.
- Enable the Pub/Sub API for that project.
You can view and manage these resources at any time in theGoogle Cloud console.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
Note: You can run the gcloud CLI in the Google Cloud console without installing the Google Cloud CLI. To run the gcloud CLI in the Google Cloud console,use Cloud Shell.gcloudinit
Set up a Google Cloud console project.
Click to:
- Create or select a project.
- Enable the Pub/Sub API for that project.
You can view and manage these resources at any time in theGoogle Cloud console.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
Note: You can run the gcloud CLI in the Google Cloud console without installing the Google Cloud CLI. To run the gcloud CLI in the Google Cloud console,use Cloud Shell.gcloudinit
Required roles
To complete this quickstart, you need the following Identity and Access Management (IAM)(IAM) roles.
To get the permissions that you need to complete this quickstart, ask your administrator to grant you thePub/Sub Editor (roles/pubsub.editor) IAM role on your project. For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Create a topic
Create a topic with the IDmy-topic:
gcloudpubsubtopicscreatemy-topic
Create a subscription
Create a subscription with the IDmy-sub and attach it tomy-topic:
gcloudpubsubsubscriptionscreatemy-sub--topic=my-topicPublish messages
Publish a message tomy-topic:
gcloudpubsubtopicspublishmy-topic--message="hello"
Receive messages
Receive the message frommy-sub:
gcloudpubsubsubscriptionspullmy-sub--auto-ack
The gcloud CLI prints the message to the command line.
How did it go?
What's next
See all the availablegcloud CLI commands for Pub/Sub
Learn more about the Pub/Subconceptsdiscussed in this page.
Read thebasics of the Pub/Sub service.
Work through anend-to-end exampleof a Pub/Sub system.
Try another Pub/Sub quickstart thatusesclient libraries or theconsole.
Choose a subscription type.
Learn more aboutPub/Sub APIs.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.