Posted: . At: 9:56 AM. This was 2 months ago. Post ID: 19351
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Install and use PostgreSQL on Ubuntu using the command line.


Installing and using PostgreSQL is not too hard using the command line on Ubuntu.

Install the PostgreSQL client.

┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:~$ sudo apt install postgresql-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-6.5.0-17 linux-headers-6.5.0-17-generic linux-image-6.5.0-17-generic linux-modules-6.5.0-17-generic linux-modules-extra-6.5.0-17-generic linux-modules-nvidia-535-6.5.0-17-generic
  linux-objects-nvidia-535-6.5.0-17-generic linux-signatures-nvidia-6.5.0-17-generic nvidia-firmware-535-535.154.05
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  postgresql-client-15 postgresql-client-common
Suggested packages:
  postgresql-15 postgresql-doc-15
The following NEW packages will be installed:
  postgresql-client postgresql-client-15 postgresql-client-common
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,294 kB of archives.
After this operation, 4,132 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 postgresql-client-common all 253 [36.4 kB]
Get:2 http://au.archive.ubuntu.com/ubuntu mantic-updates/main amd64 postgresql-client-15 amd64 15.6-0ubuntu0.23.10.1 [1,247 kB]
Get:3 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 postgresql-client all 15+253 [10.7 kB]
Fetched 1,294 kB in 1s (1,768 kB/s)       
Selecting previously unselected package postgresql-client-common.
(Reading database ... 515399 files and directories currently installed.)
Preparing to unpack .../postgresql-client-common_253_all.deb ...
Unpacking postgresql-client-common (253) ...
Selecting previously unselected package postgresql-client-15.
Preparing to unpack .../postgresql-client-15_15.6-0ubuntu0.23.10.1_amd64.deb ...
Unpacking postgresql-client-15 (15.6-0ubuntu0.23.10.1) ...
Selecting previously unselected package postgresql-client.
Preparing to unpack .../postgresql-client_15+253_all.deb ...
Unpacking postgresql-client (15+253) ...
Setting up postgresql-client-common (253) ...
Setting up postgresql-client-15 (15.6-0ubuntu0.23.10.1) ...
update-alternatives: using /usr/share/postgresql/15/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-client (15+253) ...
Processing triggers for man-db (2.11.2-3) ...

Install the PostgreSQL server.

┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:~$ sudo apt install postgresql-15
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-6.5.0-17 linux-headers-6.5.0-17-generic linux-image-6.5.0-17-generic linux-modules-6.5.0-17-generic linux-modules-extra-6.5.0-17-generic linux-modules-nvidia-535-6.5.0-17-generic
  linux-objects-nvidia-535-6.5.0-17-generic linux-signatures-nvidia-6.5.0-17-generic nvidia-firmware-535-535.154.05
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libtypes-serialiser-perl postgresql-common sysstat
Suggested packages:
  isag
The following NEW packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libtypes-serialiser-perl postgresql-15 postgresql-common sysstat
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 17.3 MB of archives.
After this operation, 48.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 libjson-perl all 4.10000-1 [81.9 kB]
Get:2 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 postgresql-common all 253 [176 kB]
Get:3 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 libcommon-sense-perl amd64 3.75-3 [20.2 kB]
Get:4 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 libtypes-serialiser-perl all 1.01-1 [11.6 kB]
Get:5 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 libjson-xs-perl amd64 4.030-2 [85.0 kB]
Get:6 http://au.archive.ubuntu.com/ubuntu mantic-updates/main amd64 postgresql-15 amd64 15.6-0ubuntu0.23.10.1 [16.4 MB]
Get:7 http://au.archive.ubuntu.com/ubuntu mantic/main amd64 sysstat amd64 12.6.1-1ubuntu1 [493 kB]
Fetched 17.3 MB in 6s (3,006 kB/s)
Preconfiguring packages ...

Now the PostgreSQL server and client are installed.

Now load PostgreSQL and create a new password for the postgres user to use the database.

┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:~$ sudo -u postgres psql postgres
could not change directory to "/home/jcartwright": Permission denied
psql (15.6 (Ubuntu 15.6-0ubuntu0.23.10.1))
Type "help" for help.
 
postgres=# \password postgres
Enter new password for user "postgres": 
Enter it again: 
postgres=#

Use this command to print help concerning all SQL commands for PostgreSQL.

postgres=# \h

Print information on all backslash commands for PostgreSQL.

postgres=# \?

Now you may create a database in PostgreSQL.

postgres=# create database foo;
CREATE DATABASE

Change the owner of the database to a certain user.

postgres=# ALTER DATABASE foo
postgres-# OWNER to postgres;
ALTER DATABASE

Now list all databases and the added database should be in the list.

postgres-# \list
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 foo       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(4 rows)

Now you are ready to use the new database. Select the “foo” database and now it is ready for input.

postgres=# \c foo;
You are now connected to database "foo" as user "postgres".

This is how easy it is to use PostgreSQL on Ubuntu and create a new user. This is different from MySQL, but this takes a bit of time to get used to. It would be easy to get a database server running using Ubuntu server and PostgreSQL.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.