How to Connect to a PostgreSQL Server from a Windows PC

In this article, we show how to connect to a PostgreSQL server from a windows computer.
There are several programs that you can use to accomplish this.
These programs include the windows command prompt (cmd), SQL shell (psql), and pgadmin.
With any of these programs, you can establish contact with a PostgreSQL database server.
Let's start with the windows command prompt.
In order to use it, you will need the postgresql software installed on your computer.
You can download the postgresql software at the following link: https://www.postgresql.org/download/
After you download the postgresql software, then you need add the path of this software to your computer's environment variables.
The 2 paths to add to the windows environment variables are shown below.
You may need to slightly modify the above, depending on where the software is installed on your computer and what version of postgresql you are using. On my computer, I have postgresql 16 installed. But the most current version as of this writing is postgresql 17. So you just have to make sure you have the right version specified.
Once this is set up, you just be able to go to the command prompt and type in, psql
Typing this in now should not result in the message, 'psql' is not recognized as an internal or external command, operable program or batch file.
You should get prompted to enter in the password for a user.
This user may or may not be the right user.
So what you need to do is type in the following line shown below.
By default, when you first install postgresql on your computer, the default database is named postgres, the default user is postgres, and the password is postgres.
Of course, later, you can change any of these names. You should definitely change the password if you are going into a production environment.
Once you log in successfully, you gain entrance into psql, as shown below.
You can now run various SQL commands to find out information about the database such as its version. You can also do things such as create tables, etc.
To show all databases, type in, \list
To show the current database that is selected, type in, SELECT current_database();
To show all tables of the database, type in, \dt
Below, we show the current selected database.
You can change this database to another by typing in the line, \connect Databasename or, for short, \c Databasename
So this is one method, using the windows command prompt software.
The next method is to use the SQL shell (psql).
This can be seen in the following image: SQL Shell in Windows
So we have to log into the postgresql database.
By default, the host is localhost. The database is postgres. The user is postgres. The password is postgres.
Once you log in successfully, you should see, the psql with the version shown, as can be seen below.
So now you can run SQL commands such as seeing the database that is currently selected.
So this is the way of doing it with the SQL shell in windows.
Another way is through pgadmin.
And this way is probably the simplest of them all.
Once you open the software and type in the password, you gain access to the database.
After that, you can go to the database, right click on it, and then click on 'PSQL Tool'
Once you click on it, it brings you to a now familiar screen.
On this page, you can enter in any SQL command, such as again getting the current
selected database.
So these are various ways we can connect and interact with a postgresql database server
in windows.
Related Resources
How to Create Your Own PostgreSQL Server on Your Own Computer in Windows