How to install and configure the PostgreSQL server in Ubuntu. This is an open-source relational database management system and it’s designed to handle workloads, from a single machine to data warehouses. In a similar way, we can configure and install PostgreSQL on Linux as well as ubuntu.
We don’t need to add any repository to install the database. we have to update the packages and simply run the below commands to get installed.
1. If you want to update the machine before installing the PostgreSQL, you can use sudo apt update command.
2. Install the PostgreSQL using apt install postgresql postgresql-contrib postgresql-client-common
3. The service will start automatically we can check using netstat -tnlp
Install PostgreSQL using the package:
The PostgreSQL must be listening on port 5432. s
We can check the version installed using apt show postgresql
we can also use the below command to Check the PostgreSQL server version.
sudo -u postgres psql -c "SELECT version();"
We can also check the service running with service postgresql status so
By default, user postgres will be created on the machine and we can log in using the below commands. so
su - postgres psql
you can exit using \q and check the database using \l
Check user using \du so
Installing Postgresql using source code:
We can download the PostgreSQL using the below command and extract it using the tar command.
wget https://ftp.postgresql.org/pub/source/v10.13/postgresql-10.13.tar.gz tar -xvf postgresql-10.13.tar.gz
Once the folder is extracted, we need to change the directory and run the following commands.
cd postgresql-10.13/ sudo ./configure sudo make sudo make install
When the above command executed successfully, you will see a message” PostgreSQL installation complete“. we need to create a user and set the password.
sudo useradd postgres sudo passwd postgres
Verify the PostgreSQL directory structure using the below command.
ls -l /usr/local/pgsql
Now, we have to create a PostgreSQL data directory and make it owner as Postgres user.
sudo mkdir /usr/local/pgsql/data sudo chown postgres:postgres /usr/local/pgsql/data sudo ls -ld /usr/local/pgsql/data/ drwxr-xr-x 2 postgres postgres 4096 Aug 29 08:09 /usr/local/pgsql/data/
Initializing the PostgreSQL database and we need to log in to the server and run the below command.
su - postgres $ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/ ## initialize the database The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /usr/local/pgsql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default timezone ... America/Los_Angeles selecting dynamic shared memory implementation ... posix creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start
Now check the data in the data directory using sudo ls -l /usr/local/pgsql/data command.
Use the postgres postmaster command to start the server in the background using the below command.
$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &