[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Quickstart: Publish and receive messages in Pub/Sub by using the gcloud CLI\n\nPublish and receive messages in Pub/Sub by using the gcloud CLI\n===============================================================\n\nThis page shows you how to do the following operations in Pub/Sub using the Google Cloud CLI:\n\n- Create a topic and subscription.\n- Publish messages to the topic.\n- Receive messages from the subscription.\n\n*** ** * ** ***\n\nTo follow step-by-step guidance for this task directly in the\nGoogle Cloud console, click **Guide me**:\n\n[Guide me](https://console.cloud.google.com/freetrial?redirectPath=/?tutorial=pubsub_quickstart_gcloud)\n\n*** ** * ** ***\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- Set up a Google Cloud console project.\n\n Set up a project \n Click to:\n - Create or select a project.\n - Enable the Pub/Sub API for that project.\n\n You can view and manage these resources at any time in the\n [Google Cloud console](https://console.cloud.google.com/).\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n | **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\n | Cloud Shell](https://console.cloud.google.com/?cloudshell=true).\n\n- Set up a Google Cloud console project.\n\n Set up a project \n Click to:\n - Create or select a project.\n - Enable the Pub/Sub API for that project.\n\n You can view and manage these resources at any time in the\n [Google Cloud console](https://console.cloud.google.com/).\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n | **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\n | Cloud Shell](https://console.cloud.google.com/?cloudshell=true).\n\n\u003cbr /\u003e\n\nCreate a topic\n--------------\n\nCreate a topic with the ID `my-topic`: \n\n```bash\ngcloud pubsub topics create my-topic\n```\n\nCreate a subscription\n---------------------\n\nCreate a subscription with the ID `my-sub` and attach it to `my-topic`: \n\n```bash\ngcloud pubsub subscriptions create my-sub --topic=my-topic\n```\n\nPublish messages\n----------------\n\nPublish a message to `my-topic`: \n\n```bash\ngcloud pubsub topics publish my-topic --message=\"hello\"\n```\n\nReceive messages\n----------------\n\nReceive the message from `my-sub`: \n\n```bash\ngcloud pubsub subscriptions pull my-sub --auto-ack\n```\n\nThe gcloud CLI prints the message to the command line.\n\nHow did it go?\n--------------\n\nIt worked! **Great!** What did you like about the quickstart? What could we have done better? [Let us know!](#).\nI got stuck. **We're sorry to hear that.** [Let us know what went wrong](#). We'll want to fix it.\n\nWhat's next\n-----------\n\n- See all the available\n [gCloud CLI commands](/sdk/gcloud/reference/pubsub) for Pub/Sub\n\n- Learn more about the Pub/Sub [concepts](/pubsub/docs/overview)\n discussed in this page.\n\n- Read the [basics of the Pub/Sub service](/pubsub/docs/pubsub-basics).\n\n- Work through an [end-to-end example](/pubsub/docs/building-pubsub-messaging-system)\n of a Pub/Sub system.\n\n- Try another Pub/Sub quickstart that\n uses [client libraries](/pubsub/docs/publish-receive-messages-client-library)\n or the [console](/pubsub/docs/publish-receive-messages-console).\n\n- Learn how to create [topics](/pubsub/docs/create-topic)\n and publish [messages](/pubsub/docs/publisher).\n\n- [Choose](/pubsub/docs/subscriber) a subscription type.\n\n- Learn more about [Pub/Sub APIs](/pubsub/docs/reference/service_apis_overview)."]]