Quickstart: Connect and query PostgreSQL using Azure Data Studio
This quickstart shows how to use Azure Data Studio to connect to Postgres, and then use SQL statements to create the database tutorialdb and query it.
To complete this quickstart, you need Azure Data Studio, the PostgreSQL extension for Azure Data Studio, and access to a PostgreSQL server.
- Install Azure Data Studio.
- Install the PostgreSQL extension for Azure Data Studio.
- Install PostgreSQL. (Alternatively, you can create a Postgres database in the cloud using az postgres up).
Connect to PostgreSQL
Start Azure Data Studio.
The first time you start Azure Data Studio the Connection dialog opens. If the Connection dialog doesn't open, click the New Connection icon in the SERVERS page:
In the form that pops up, go to Connection type and select PostgreSQL from the drop-down.
Fill in the remaining fields using the server name, user name, and password for your PostgreSQL server.
Setting Example value Description Server name localhost The fully qualified server name User name postgres The user name you want to log in with. Password (SQL Login) password The password for the account you are logging in with. Password Check Check this box if you don't want to enter the password each time you connect. Database name <Default> Fill this if you want the connection to specify a database. Server group <Default> This option lets you assign this connection to a specific server group you create. Name (optional) leave blank This option lets you specify a friendly name for your server.
After successfully connecting, your server opens in the SERVERS sidebar.
Create a database
The following steps create a database named tutorialdb:
Right-click on your PostgreSQL server in the SERVERS sidebar and select New Query.
Paste this SQL statement in the query editor that opens up.
CREATE DATABASE tutorialdb;
From the toolbar select Run to execute the query. Notifications appear in the MESSAGES pane to show query progress.
You can use F5 on your keyboard to execute the statement instead of using Run.
After the query completes, right-click Databases and select Refresh to see tutorialdb in the list under the Databases node.
Create a table
The following steps create a table in the tutorialdb:
Change the connection context to tutorialdb using the drop-down in the query editor.
Paste the following SQL statement into the query editor and click Run.
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.
-- Drop the table if it already exists DROP TABLE IF EXISTS customers; -- Create a new table called 'customers' CREATE TABLE customers( customer_id SERIAL PRIMARY KEY, name VARCHAR (50) NOT NULL, location VARCHAR (50) NOT NULL, email VARCHAR (50) NOT NULL );
Paste the following snippet into the query window and click Run:
-- Insert rows into table 'customers' INSERT INTO customers (customer_id, name, location, email) VALUES ( 1, 'Orlando', 'Australia', ''), ( 2, 'Keith', 'India', 'firstname.lastname@example.org'), ( 3, 'Donna', 'Germany', 'email@example.com'), ( 4, 'Janet', 'United States','firstname.lastname@example.org');
Query the data
Paste the following snippet into the query editor and click Run:
-- Select rows from table 'customers' SELECT * FROM customers;
The results of the query are displayed:
Learn about the scenarios available for Postgres in Azure Data Studio.