How To Connect to PostgreSQL Database in Visual Studio Code and Run SQL Queries (2024)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can use your Visual Studio code to query your post gray SQL Server so let's get started so first of all open the extensions section in Visual Studio code and search for the extension called SQL tools and the first result most probably which will appear here will be this one which is for SQL tools now this extension works with many other s SQL based database systems like MySQL postr SQL Microsoft SQL Server so the full list of supported database systems you will be able to see in the read me section of uh this extension so first of all click on install which is going to install this extension now once this extension is installed there is a new icon which will appear here on the left hand bar R which is for SQL tools so I'm going to click on this uh icon here and let me close this uh extension and then I can click on new connection to create a new connection now in order to connect to the post gray SQL or any other database system this SQL tool extension requires the desired driver extension for that database system so in our case we will will be connecting the post gr SQL right so we need the driver extension which is for post G SQL so once again go to the extensions and then once again search for SQL tools and under SQL tools you will be able to see more options which are related to SQL tools so for example SQL tool for MySQL Maria DB SQL tools for uh sqlite and we can also o see the SQL tools for postc SQL which is the required driver we need to connect to the SQL Server so just install this extension also if you're using any other database then you need to install the required driver for that database which is related to SQL tools so once this uh is installed we can once again click on SQL tools option here and now when I click on add new connection I can see these two options I will click on post gr SQL here and then I need to provide the configuration of my post gray SQL Server so that I can connect to my post cray so here the connection name you can uh give any name here I'm using the post CR locally so I'm going to just uh name my connection as PG local here and then you can also provide a connection group I'm going to leave it as default now you can connect via the server and Port socket file or connection string I'm going to use the first option which is server and the port and then you need to provide the server address if you are running your postgre SQL locally just leave it as Local Host if you're running your post gr SQL server on some remote machine then provide the proper IP address of that machine and then the post Port the default port for connecting to the post SQL Server is 5432 if you are using some different port for your MySQL server just provide that here now in order to connect to your postre SQL Server you need to provide the name of the database which already pre-exist in your MySQL server so let's say I have this database which is called demo which I have already created in my MySQL server so I need to provide the database name I want to connect to so here you need to provide the username which you use to connect to your MySQL server generally uh if you have installed postra freshly then the username will be postrace but it can be different also which depends on the settings of your post G SQL Server now here in the password section you have few different options like uh ask on connect use empty password and then you have save as plain text in settings so I'm going to use the last option which is going to save my password as the empty setting just for the Simplicity but if you are concerned about the security then I will ask you to use uh the first option or the second option here so I'm going to choose the last option which is save as plain text in settings because I have my post server running on my local machine so my password is really simple here and then when I move down a little it says node PG driver specific option if you're using SSL then you can uh enable the SSL from here also you will be able to see these multiple options like query timeout statement timeout connection timeout if you want to give these uh uh settings then you can give all these settings from here I'm going to leave everything as default and then you have this connection timeout option so I'm going to give this connection time out as 30 here and now I have given all the required configurations so here I have the option to save my connection but before that we can also test our connection by clicking on the test connection button so when I click on the test connection button I can see successfully connected that means my connection works so I can click on Save connection now as you can see the list of connection will be listed here and your configuration in the form of Json is listed here so you can see my password for my local post gr is root and uh all the other configuration is listed here now in order to connect to your postra server you can just click on connect now or you can click on this icon here which is for connect you can also see the query history which we are going to see little bit later when we provide some queries here so I'm going to click on this connect icon here which is going to connect to my post SQL database and it's going to give me this kind of interface where I can run queries on my database I can also expand this connection where I will be able to see my database which is a demo and then if I have some uh tables I will be able to see the list of tables here so I already have one table here which is for the names if I want to create new tables I can provide the query to create those tables so let's say I want to create a new table here in addition to the names table so I can uh provide this query which is create table if not exist the table name will be users and it has three columns ID username and email the ID is the primary key here now in order to run this query you can can click on run on active connection and once I uh click on this it's going to show me this kind of uh tab here which is for my query and then when I refresh uh this connection here so I'm going to click on refresh here then I can see my users table and then I can also see that I have these three columns in my table right now if I want to see the content of this table I can right click on this uh table and then click on show table record right now I don't have anything so I can see the blank uh table here but once I have some data in this table I will be able to see the records so now let's insert some data into the table so I'm going to provide this query for inserting the data into my post crit table which is insert into users user name comma email and then I am going to add these three row in my table now if you want to uh run a particular query and not the whole SQL script then you can select your query here and then right click on your query and then you have this option which says run selected query here and it's going to run your selected query and once that's successful I can once again right click on the users table and then click on show table records and now I I can see three records here which are added to my table right so this is uh the SQL session which you can save uh for the later references also you can see all the queries which are executed in the query history here now let's say I want to select the record via the query and not by right clicking on this table I can do that also so let's say when I type some query here let's say SE this extension is going to give me the options for the auto complete for example select srix from users and then I can once again select this and then right click and then click on run selected query and it's going to show me the table records now if you want to see details about your table or database you can once again right click on your your uh table or the database and then you have the option to describe your table it's going to describe what are the columns which are available in your table and what are their data types and uh other constraints about your columns you can also add a new insert query so just go to the end of your SQL script and then right click on your table and then you can click on generate ins insert query is going to give you the sample insert query for you so as I mentioned ID is not required in my table which Auto increments so I'm going to remove that but I can change the values for the username and email right so let's say a username I'm going to leave it as default and email let's say I'm going to say email atate gmail.com and then I can once again execute this uh query you know the drill and once I do that the data is inserted and I can see the new data here like this which is the fourth row now let's say you want to change the configuration of your SQL connection let's say your Port has changed and you want to change the SQL connection just so just right click on your connection here and then click on edit connection and you will will be able to edit your connection you also have the option to delete your connection or you also have the option to disconnect from your current connection right so when I click on disconnect it's disconnected and now I'm no longer connected to my post csql server but the configuration is there so let's say at the later point when you want this connection once again click on connect and it will connect once again this is how you can use Visual Studio code to connect to your postc SQL Server then query your postc SQL server using visual studio code that's it for this video I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 3,975
Rating: undefined out of 5
Keywords: How do I run a SQL query in VS Code?, How to connect to Postgres database in Visual Studio?, How to run PostgreSQL database?, How to run SQL query in PostgreSQL?, How to run PostgreSQL in VS Code?, How do I run a SQL query in a database?, How to script PostgreSQL database?, How to connect PostgreSQL database using command?, How to run MySQL in Visual Studio Code?, How do I run a SQL script in pgAdmin?, How to open PostgreSQL database in terminal?
Id: cc-cSSsGqbA
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Sun Feb 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.