PostgreSQL or Postgres is an open-source general-purpose object-relational database management system with many advanced features that allows you to build fault-tolerant environments or complex applications.
In this guide, we will explain how to install the PostgreSQL database server on Ubuntu 20.04 server, and explore the basics of PostgreSQL database administration.
Step 1: Installing PostgreSQL
PostgreSQL is available from Ubuntu’s default software repositories, So, use the below command to check the available versions to install:
$ sudo apt update £ sudo apt list postgresql*
02- Run the following commands to install PostgreSQL server on Ubuntu:
$ sudo apt update sudo apt install postgresql postgresql-contrib
03- Once the installation is done, use the below command to Start/Enable PostgreSQL:
$ sudo systemctl enable --now postgresql
04- Use the psql
tool to verify the installation is finished properly, by connecting to the PostgreSQL database server and printing the version:
$ sudo -u postgres psql -c "SELECT version();" PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
Step 2: Accessing Postgresql Database
– Switch into the postgres user:
$ su – postgres
– Connect to the PostgreSQL terminal:
$ psql
– Exit from PosgreSQL prompt like below:
# \q
Step 3: Usage Examples
– List all the PosgreSQL databases by running the following command:
# \list
– Connect to a database:
# \c database_name
– List all the tables in the selected PosgreSQL database:
# \d
– Sot to create a Database in PosgreSQL use the below command:
# createdb database_name # createdb database_name OWNER rolename;
– Create a table in the selected PosgreSQL database:
# create table employees (name varchar(25), surname varchar(25));
– Insert records into a a table by typing the below command:
# INSERT INTO employees VALUES ('Lotfi','waderni');
Conclusion
You are learned how to set up PostgreSQL on your Ubuntu 20.04 LTS server. However, there is much more to learn with Postgres. For more information visit the PostgreSQL Documentation
Here are some more guides that may interest you:
1 comment
Hi, thank you so much YallaLabs Team for the great guide. I installed successfully postgres on my Ubuntu 20 thanks to you guys.