Introduction
psql is the standard command-line interface for interacting with a PostgreSQL. This article will explain how to install psql(Postgres) on various platforms.
Before you begin
Before you begin, make sure that you don't already have psql installed. If you've ever installed Postgres earlier, you likely already have psql installed.
psql --version
Output:
psql (PostgreSQL) 12.3
Install Postgres on macOS using Homebrew
Firstly, we need to install the Brew Package Manager. You can ignore this if you already have Brew Package Manager installed.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Secondly, you'll have to update brew. To update brew run the following command from your command lines and then the last comman brew install postgresql
will install psql:
brew doctor
brew update
brew install postgresql
Install Postgres on Ubuntu
Install on Ubuntu using the apt package manager:
sudo apt-get update
sudo apt-get install postgresql-client
Note: This installs the psql client only and not the PostgreSQL database.
Install Postgres on Windows
I recommend using the installer from PostgreSQL.org.
Last step: Connect to your PostgreSQL server Let's confirm that psql is successfully installed:
psql --version
Output:
psql (PostgreSQL) 12.3
To connect to the PostgreSQL server, we need the following connection params:
- Hostname
- Port
- Username
- Password
- Database name
There are mainly two ways to use these params.
- First Option:
psql -h [HOST_NAME] -p [PORT] -U [USER_NAME] -W -d [DATABASE_NAME]
Once you run the above command, the prompt will ask you for your password. (Which was specified with the -W flag.)
- Second Option:
psql postgres://[USER_NAME]:[PASSWORD]@[HOST_NAME]:[PORT]/[DATABASE_NAME]?sslmode=require
Conclusion
I hope this article helped you to install psql client in your system. If it does please share so that others will also find it useful.
You may also like my other articles:
- Strapi.js - Open Source Node.js Headless CMS
- Things to keep in mind before starting Javascript framework
- ES2020/ES10: The Next Big Update! Awesome New JavaScript Features With Examples
If you liked the article, feel free to share it to help others find it!
You may also follow me on LinkedIn and X
💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter here.
Discussions