|
| 1 | +--- |
| 2 | +title: "Quickstart: Connect and query PostgreSQL" |
| 3 | +titleSuffix: Azure Data Studio |
| 4 | +description: This quickstart shows how to use Azure Data Studio to connect to PostgreSQL and run a query |
| 5 | +ms.custom: "seodec18" |
| 6 | +ms.date: "03/19/2019" |
| 7 | +ms.prod: sql |
| 8 | +ms.technology: azure-data-studio |
| 9 | +ms.reviewer: "alayu; sstein" |
| 10 | +ms.topic: "quickstart" |
| 11 | +author: "rachel-msft" |
| 12 | +ms.author: "raagyema" |
| 13 | +manager: craigg |
| 14 | +--- |
| 15 | + |
| 16 | +# Quickstart: Connect and query PostgreSQL using [!INCLUDE[name-sos](../includes/name-sos-short.md)] |
| 17 | +This quickstart shows how to use [!INCLUDE[name-sos](../includes/name-sos-short.md)] to connect to Postgres, and then use SQL statements to create the database *tutorialdb* and query it. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +To complete this quickstart, you need [!INCLUDE[name-sos](../includes/name-sos-short.md)], the PostgreSQL extension for [!INCLUDE[name-sos](../includes/name-sos-short.md), and access to a PostgreSQL server. |
| 22 | + |
| 23 | +- [Install [!INCLUDE[name-sos](../includes/name-sos-short.md)]](download.md). |
| 24 | +- [Install the PostgreSQL extension for Azure Data Studio](postgres-extension.md). |
| 25 | +- [Install PostgreSQL](https://www.postgresql.org/download/). (Alternatively, you can create a Postgres database in the cloud using [az postgres up](https://docs.microsoft.com/azure/postgresql/quickstart-create-server-up-azure-cli)). |
| 26 | + |
| 27 | +## Connect to PostgreSQL |
| 28 | + |
| 29 | +1. Start **[!INCLUDE[name-sos](../includes/name-sos-short.md)]**. |
| 30 | + |
| 31 | +2. The first time you start [!INCLUDE[name-sos](../includes/name-sos-short.md)] the **Connection** dialog opens. If the **Connection** dialog doesn't open, click the **New Connection** icon in the **SERVERS** page: |
| 32 | + |
| 33 | +  |
| 34 | + |
| 35 | +3. In the form that pops up, go to **Connection type** and select **PostgreSQL** from the drop-down. |
| 36 | + |
| 37 | + |
| 38 | +4. Fill in the remaining fields using the server name, user name, and password for your PostgreSQL server. |
| 39 | + |
| 40 | +  |
| 41 | + |
| 42 | + | Setting | Example value | Description | |
| 43 | + | ------------ | ------------------ | ------------------------------------------------- | |
| 44 | + | **Server name** | localhost | The fully qualified server name | |
| 45 | + | **User name** | postgres | The user name you want to log in with. | |
| 46 | + | **Password (SQL Login)** | *password* | The password for the account you are logging in with. | |
| 47 | + | **Password** | *Check* | Check this box if you don't want to enter the password each time you connect. | |
| 48 | + | **Database name** | \<Default\> | Fill this if you want the connection to specify a database. | |
| 49 | + | **Server group** | \<Default\> | This option lets you assign this connection to a specific server group you create. | |
| 50 | + | **Name (optional)** | *leave blank* | This option lets you specify a friendly name for your server. | |
| 51 | + |
| 52 | +5. Select **Connect**. |
| 53 | + |
| 54 | +After successfully connecting, your server opens in the **SERVERS** sidebar. |
| 55 | + |
| 56 | + |
| 57 | +## Create a database |
| 58 | + |
| 59 | +The following steps create a database named **tutorialdb**: |
| 60 | + |
| 61 | +1. Right-click on your PostgreSQL server in the **SERVERS** sidebar and select **New Query**. |
| 62 | + |
| 63 | +2. Paste this SQL statement in the query editor that opens up. |
| 64 | + |
| 65 | + ```sql |
| 66 | + CREATE DATABASE tutorialdb; |
| 67 | + ``` |
| 68 | + |
| 69 | +3. From the toolbar select **Run** to execute the query. Notifications appear in the **MESSAGES** pane to show query progress. |
| 70 | + |
| 71 | +>[!TIP] |
| 72 | +> You can use **F5** on your keyboard to execute the statement instead of using **Run**. |
| 73 | +
|
| 74 | +After the query completes, right-click **Databases** and select **Refresh** to see **tutorialdb** in the list under the **Databases** node. |
| 75 | + |
| 76 | + |
| 77 | +## Create a table |
| 78 | + |
| 79 | + The following steps create a table in the **tutorialdb**: |
| 80 | + |
| 81 | +1. Change the connection context to **tutorialdb** using the drop-down in the query editor. |
| 82 | + |
| 83 | +  |
| 84 | + |
| 85 | +2. Paste the following SQL statement into the query editor and click **Run**. |
| 86 | + |
| 87 | + > [!NOTE] |
| 88 | + > You can either append this or overwrite the existing query in the editor. Clicking **Run** executes only the query that is highlighted. If nothing is highlighted, clicking **Run** executes all queries in the editor. |
| 89 | +
|
| 90 | + ```sql |
| 91 | + -- Drop the table if it already exists |
| 92 | + DROP TABLE IF EXISTS customers; |
| 93 | + -- Create a new table called 'customers' |
| 94 | + CREATE TABLE customers( |
| 95 | + customer_id SERIAL PRIMARY KEY, |
| 96 | + name VARCHAR (50) NOT NULL, |
| 97 | + location VARCHAR (50) NOT NULL, |
| 98 | + email VARCHAR (50) NOT NULL |
| 99 | + ); |
| 100 | + ``` |
| 101 | + |
| 102 | +## Insert rows |
| 103 | + |
| 104 | +Paste the following snippet into the query window and click **Run**: |
| 105 | + |
| 106 | + ```sql |
| 107 | + -- Insert rows into table 'customers' |
| 108 | + INSERT INTO customers |
| 109 | + (customer_id, name, location, email) |
| 110 | + VALUES |
| 111 | + ( 1, 'Orlando', 'Australia', ''), |
| 112 | + ( 2, 'Keith', 'India', 'keith0@adventure-works.com'), |
| 113 | + ( 3, 'Donna', 'Germany', 'donna0@adventure-works.com'), |
| 114 | + ( 4, 'Janet', 'United States','janet1@adventure-works.com'); |
| 115 | + ``` |
| 116 | + |
| 117 | +## Query the data |
| 118 | + |
| 119 | +1. Paste the following snippet into the query editor and click **Run**: |
| 120 | + |
| 121 | + ```sql |
| 122 | + -- Select rows from table 'customers' |
| 123 | + SELECT * FROM customers; |
| 124 | + ``` |
| 125 | + |
| 126 | +2. The results of the query are displayed: |
| 127 | + |
| 128 | +  |
| 129 | + |
| 130 | +## Next Steps |
| 131 | + |
| 132 | +Learn about the [scenarios available for Postgres in Azure Data Studio](postgres-extension.md). |
0 commit comments