The PostgreSQL Development Team has announced the latest version PostgreSQL 12. PostgreSQL is an open-source object-relational database system. It is one of the leading database servers used for production servers. PostgreSQL allows us to execute stored procedures in various programming languages, like C/C++, Python, Java, Perl, Ruby, and its own PL/pgSQL, which is similar to Oracle’s PL/SQL. This tutorial will help you with installing the PostgreSQL database server on your Ubuntu 18.04 LTS, Ubuntu 16.04 LTS, and 14.04 LTS systems.

Advertisement

Step 1 – Enable PostgreSQL Apt Repository

PostgreSQL packages are also available in default Ubuntu repository. So you need to add PostgreSQL apt repository to your system suggested on official PostgreSQL website using following command.

Start with the import of the GPG key for PostgreSQL packages.

sudo apt-get install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Now add the repository to your system.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Step 2 – Install PostgreSQL on Ubuntu

Now as we have added PostgreSQL official repository in our system, First we need to update the repository list. After that install Latest PostgreSQL Server in our Ubuntu system using the following commands.

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Multiple other dependencies will also be installed. PostgreSQL 12 is the latest available version during the last update of this tutorial.

Install postgresql 11 on ubuntu

Step 3 – Create User for PostgreSQL

By default, PostgresQL creates a user ‘postgres’ with the role ‘postgres’. It also creates a system account with the same name ‘postgres’. So to connect to Postgres server, log in to your system as user postgres and connect the database.

sudo su - postgres
psql

Now configure PostgreSQL to make is accessible by your normal users. Change your_username with your actual user already created on your Ubuntu system.

postgres-# CREATE ROLE your_username WITH LOGIN CREATEDB ENCRYPTED PASSWORD 'your_password';
postgres-# \q

Then switch to the user account and run createdb command followed by the database name. This will create a database on PostgreSQL.

su - your_username 
createdb my_db

After that connect to the PostgreSQL server. You will be logged in and get database prompt. To list all available databases use these commands.

psql

rahul=> \list
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 my_db     | rahul    | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres

To disconnect from PostgreSQL database command prompt just type below command and press enter. It will return you back to the Ubuntu command prompt.

postgres-# \q

Conclusion

Your PostgreSQL installation has been completed successfully. Let’s move to install Graphical user interface for PostgreSQL like pgAdmin4 and phpPgAdmin of Ubuntu systems.

Share.

39 Comments

  1. Rahul, I owe you! Many thanks!
    This post really got me out of a headache trying to understand the basics of setting up postgresql.

    I have 2 questions though.

    1 ) step 3 : I create a user that is the sudo user of the computer:

    postgres-# CREATE ROLE your_username WITH LOGIN CREATEDB ENCRYPTED PASSWORD ‘my_postgres_password’;

    But when I try to log with “$ su – my_user_name”, I am expected to enter my system password. When do I need the “my_postgres_password”?

    2) When I run “psql my_usr_name” the first time, it did not work because psql was expecting a database name called “my_usr_name” and not “myDB”. I had to create a database with the name of the user. It works now, but I find that peculiar. In your example, you have no db called “rahul”.

  2. How can I know what the password is and how can I change it?
    I tried : sudo -u postgres psql
    but got : postgres is not in the sudoers file. This incident will be reported.

  3. I get an apt error:

    E: The repository ‘http://apt.postgresql.org/pub/repos/apt sylvia-pgdg Release’ does not have a Release file

    Is there a way to fix this?

    • Hi. Do you use Mint? Please, run this:

      root@mint2:/home/shmakovpn# cat /etc/os-release
      NAME=”Linux Mint”
      VERSION=”19 (Tara)”
      ID=linuxmint
      ID_LIKE=ubuntu
      PRETTY_NAME=”Linux Mint 19″
      VERSION_ID=”19″
      HOME_URL=”https://www.linuxmint.com/”
      SUPPORT_URL=”https://forums.ubuntu.com/”
      BUG_REPORT_URL=”http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/”
      PRIVACY_POLICY_URL=”https://www.linuxmint.com/”
      VERSION_CODENAME=tara
      UBUNTU_CODENAME=bionic
      root@mint2:/home/shmakovpn#

      Then run
      root@mint2:/home/shmakovpn# lsb_release -cs
      tara
      — — —
      Thus you should use UBUNTU_CODENAME instead of VERSION_CODENAME

      Try this
      cat /etc/os-release | grep UBUNTU_CODENAME | sed ‘s/UBUNTU_CODENAME=//’
      bionic (in my case)
      —–
      So the solution is
      sudo sh -c ‘echo “deb http://apt.postgresql.org/pub/repos/apt/ `cat /etc/os-release | grep UBUNTU_CODENAME | sed s/UBUNTU_CODENAME=//`-pgdg main” > /etc/apt/sources.list.d/pgdg.list’
      Enjoy

  4. Thank you. However, as far as I know phpPgAdmin is not supporting PostgreSQL 10. Please take that into account.

  5. We installed 9.6 but it did not get loaded completely or correctly. Learned that a few directories that should have had something in them were empty, more or less confirming that the install was incorrect. With your instructions we were able to get it loaded correctly.

    Thanks!

  6. I do all the steps but I need the graphic interface. When I search for “postgre” in my computer it seems that postgresql is not installed!
    Instead, when I do all the last steps it seems that it is stalled, so I don’t understand anything:

    user@COMPUTER2:~$ sudo su – postgres
    [sudo] password for user:
    postgres@COMPUTER2:~$ psql
    psql (9.5.1)
    Type “help” for help.

    postgres=# conninfo
    You are connected to database “postgres” as user “postgres” via socket in “/var/run/postgresql” at port “5432”.
    postgres=#

    How can I install the graphic interface?

    Thank you!

Reply To Ali Cancel Reply

Exit mobile version