If you have followed the steps from the Install PostgreSQL page, you now have a PostgreSQL database on you computer.
There are several ways to connect to the database, we will look at two ways in this tutorial:
Both of them comes with the installation of PostgreSQL
SQL Shell (psql) is a terminal based program where you can write and execute SQL syntax in the command-line terminal.
You will find the SQL Shell (psql) tool in the start menu under PostgreSQL:
Tip: If you cannot find it, try searching for "SQL Shell" on your computer.
Once the program is open, you should see a window like the one below.
Insert the name of the server.
The suggested choice is [localhost], which is correct, press [Enter] to accept:
The suggested database is [postgres], which is correct, press [Enter] to accept:
The suggested port is [5432], which is correct, at least in my case, press [Enter] to accept:
The suggested username is [postgres], which is correct, at least for me, press [Enter] to accept:
Enter the password you chose when you installed the PostgreSQL database, my password is 12345678:
The result might look like an error, but if it shows psql (15.2)
or any other version, and in the end you see the postgres=#
command (and maybe a warning in between), then you have successfully connected to the database!
Once you have connected to the database, you can start executing SQL statements.
Our database is empty, so we cannot query any tables yet, but we can check the version with this SQL statement:
SELECT version();
To insert SQL statements in the SQL Shell command, just write them after the postgres=#
command like this:
Press [Enter] and the result should look like this:
Note: Always end SQL statements with a semicolon ;
SQL Shell waits for the semicolon and executes all lines as one SQL statement.
A multiple lines SQL statement is not executed before we include a semicolon at the end.
Same statement, but in two lines:
Now we are ready to create tables and fill them with data, but first we want to take a look at an application called pgAdmin 4.
Learn more about pgAdmin 4 in the next chapter.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!