Please Note: This article is part of our historical archive. Because it was published a while ago, some of the information, links, or context may now be outdated.
Wondering how to configure vultr postgresql? We can help you.
At Bobcares, we offer solutions for every query, big and small, as a part of our Server Management Service.
Let’s take a look at how our Support Team help a customer to configure vultr postgresql
How to configure vultr postgresql?
PostgreSQL is one of the most advanced open source Relational Database Management Systems.
Today, let us see steps followed by our Support Techs to configure vultr postgresql
1.Install the PostgreSQL Server
Start by updating your Ubuntu’s server package information index.
$ sudo apt -y updateThen, install the PostgreSQL core database server, command-line client, and additional dependencies.
$ sudo apt install -y postgresql postgresql-contribNext, verify the PostgreSQL installation.
$ dpkg --status postgresqlMake sure your output is similar to the one shown below.
You should have PostgreSQL version 12.
Package: postgresql Status: install ok installed ... Version: 12+214ubuntu0.1 ...
- PostgreSQL runs on port 5432.
- The default configuration file is located here:
/etc/postgresql/12/main/postgresql.conf - PostgreSQL creates all databases in this directory:
/var/lib/postgresql/12/main
The PostgreSQL database server runs as a service under the name
postgresql. Manage the service by running the commands below.
- Firstly, stop PostgreSQL server:
$ sudo systemctl stop postgresql - Start PostgreSQL server:
$ sudo systemctl start postgresql - Restart PostgreSQL(e.g. after changing configuration settings) server:
$ sudo systemctl restart postgresql - Reload PostgreSQL server:
$ sudo systemctl reload postgresql - Finally, check PostgreSQL status:
$ sudo systemctl status postgresql
You’ve successfully installed the PostgreSQL database server.
Before you begin using it, you’ll secure the root user with a password in the next step.
2.Configure the PostgreSQL Server
By default, PostgreSQL ships with
psql. This is a command-line client that you can use to log in to the database server.
The installation setup also creates a UNIX user named
postgres. This is the PostgreSQL server super-user or root user. Firstly, to log in to the PostgreSQL server via the
psql command-line client as the postgres user, use the command below. $ sudo -u postgres psqlUnder the hood, the statement above switches to the
UNIX USER and runs thepostgrescommand.psqlOnce you execute the command, you will get the PostgreSQL prompt below.
This means your PostgreSQL server is ready to receive SQL commands.
postgres=#When you install PostgreSQL for the first time, a password for the super-user is not set by default.
To create the password, execute the statement below.
postgres=# \password postgresYou will get the prompt below. Enter a strong password and confirm it.
Enter new password: EXAMPLE_PASSWORD Enter it again: EXAMPLE_PASSWORDOutput.
postgres=#Then, exit from the
command-line client.psqlpostgres=# \qTo test the new password, edit the
configuration file using/etc/postgresql/12/main/pg_hba.conf.nano$ sudo nano /etc/postgresql/12/main/pg_hba.confLocate the line
as shown in the below content.local all postgres peer... # Database administrative login by Unix domain socket local all postgres peer # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only ...Change the authentication method from
topeerso that the line readsmd5.local all postgres md5... # Database administrative login by Unix domain socket local all postgres md5 # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only ...Save and close the file by pressing CTRL + X, then Y and ENTER.
Then, restart the
service to load the new settings.postgresql$ sudo systemctl restart postgresqlTry to log in to the PostgreSQL server again; this time around, you should prompt to enter a password.
$ sudo -u postgres psqlEnter the PostgreSQL server password that you created earlier and press ENTER to continue.
You should now logged in to the PostgreSQL server. Make sure you get the command-line client prompt as shown below.
postgres=#Once you’ve protected your PostgreSQL server with a password, you can now create a sample database and perform some data manipulation on it.
3.Create a PostgreSQL Database
Use the
command to create your firstCREATE DATABASEdatabase.test_dbpostgres=# CREATE DATABASE test_db;Output.
CREATE DATABASETo list the databases, use the
command.\lpostgres=# \lYour new
database should be listed as shown below.test_dbName | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+---------+----------------------- ... test_db | postgres | UTF8 | C.UTF-8 | C.UTF-8 |To switch to the
database, use thetest_dbcommand.\cpostgres=# \c test_db;The syntax below should also work.
postgres=# \connect test_db;Ensure you get an output showing that you’ve been connected to the
database.test_dbYou are now connected to database "test_db" as user "postgres". test_db=#This means your
database is ready, and you can proceed to create a table in the next step.test_db
4.Create a PostgreSQL Table and Perform CRUD operations
Create your first table named
under thecustomersdatabase.test_dbUse the
statement to create anSERIALcolumn to store theauto-increment.customer_id'stest_db-# CREATE TABLE customers ( customer_id SERIAL PRIMARY KEY, first_name VARCHAR (50), last_name VARCHAR (50), phone VARCHAR (10) );Output.
CREATE TABLE
List PostgreSQL Tables
Run the
command to list PostgreSQL tables.\dttest_db-# \dt;Your
table should now be present on the list of relations, as shown below.customersList of relations Schema | Name | Type | Owner --------+-----------+-------+---------- public | customers | table | postgres (1 row)
Describe a PostgreSQL Table
To get a description of the
table, use the `\ d’ command.customerstest_db=# \d customers;You can now see the structure of your
table as shown below.customersColumn | Type | Collation | Nullable | Default -------------+-----------------------+-----------+----------+------------------------------------------------ customer_id | integer | | not null | nextval('customers_customer_id_seq'::regclass) first_name | character varying(50) | | | last_name | character varying(50) | | | phone | character varying(10) | | | Indexes: "customers_pkey" PRIMARY KEY, btree (customer_id)Press Q to get back to the
prompt.test_db=#
Insert Data to the PostgreSQL Table
Run the SQL commands one by one to insert sample data into the
table.customerstest_db=# INSERT INTO customers(first_name, last_name, phone) VALUES ('JOHN', 'DOE', '11111'); test_db=# INSERT INTO customers(first_name, last_name, phone) VALUES ('MARY', 'ROE', '33333'); test_db=# INSERT INTO customers(first_name, last_name, phone) VALUES ('JANE', 'SMITH', '55555');Ensure you get the output below after executing each
statement.INSERTINSERT 0 1 ...
Display Data from a PostgreSQL Table
Run a
statement against theSELECTtable to display the records that you’ve inserted.customerstest_db=# SELECT * FROM customers;You will get a list of customers as shown below.
customer_id | first_name | last_name | phone -------------+------------+-----------+------- 1 | JOHN | DOE | 11111 2 | MARY | ROE | 33333 3 | JANE | SMITH | 55555 (3 rows)
Update Data in a PostgreSQL Table
To edit the data in a PostgreSQL table, use the
andUPDATEstatements together.WHEREFor instance, to update
phone toJOHN DOE'sfrom88888, execute the command below.11111test_db=# UPDATE customers SET phone = '88888' WHERE customer_id = 1;Output.
UPDATE 1Confirm if the
statement was executed successfully by running aUPDATEstatement against the record.SELECTtest_db=# SELECT * FROM customers WHERE customer_id = 1;As you can see from the output below,
phone number has been updated toJOHN DOE's.88888customer_id | first_name | last_name | phone -------------+------------+-----------+------- 1 | JOHN | DOE | 88888 (1 row)
Delete Record From a PostgreSQL Table
Execute the
statement to delete a record in a PostgreSQL database table.DELETEFor instance, to delete
from theMARY ROEtable, use the command below.customerstest_db=# DELETE FROM customers WHERE customer_id = 2;Output.
DELETE 1To confirm the deletion, issue the
statement against theSELECTtable.customerstest_db=# SELECT * FROM customers;You can now see that
record is missing from the table.MARY ROE'scustomer_id | first_name | last_name | phone -------------+------------+-----------+------- 3 | JANE | SMITH | 55555 1 | JOHN | DOE | 88888 (2 rows)Exit from the PostgreSQL command-line interface.
test_db=# \qIn the next step, you’ll learn how to backup and restore a PostgreSQL database.
5. Backup and Restore PostgreSQL Database
PostgreSQL comes with useful tools for creating backups and restoring databases from dump files.
Backup PostgreSQL Database
To create a compressed backup for your
, use thetest_dbutility.pg_dump$ pg_dump -d test_db -U postgres | gzip > test_db_backup.sql.gzWhen prompted, enter the super-user password for your PostgreSQL database server and press ENTER to proceed.
The
should finalize dumping and compressing the database.pg_dumpCreate a plain-text backup by executing the command below.
$ pg_dump -U postgres -f test_db_backup.sql test_dbAgain, key in the password for your PostgreSQL server and press ENTER to continue.
A plain text SQL file should be created.
You can confirm the creation of the above backup files by listing the current files from your working directory.
$ ls -lsaYour backup files should be listed as shown below.
... ... test_db_backup.sql ... test_db_backup.sql.gz ...After creating the dump files, you will restore your database to its original state in the next step.
Restore PostgreSQL Database
Log in to the PostgreSQL database server as a super-user.
$ sudo -u postgres psqlWhen prompted, enter your password and press ENTER to proceed.
Next, switch to the
and drop thetest_dbtable that you created earlier.customersYou’ll try to recreate this table again from the backups.
postgres=# \c test_db; postgres=# DROP TABLE customers;Then, exit from the PostgreSQL command-line interface by executing the
command.\qpostgres=# \qNext, issue either of the commands below to restore the database to its original state from the compressed backup file.
- Restore from compressed backup:
$ gunzip -c test_db_backup.sql.gz | psql -U postgres -d test_db- Restore from plain text SQL file that you created ealier.
$ psql -U postgres -d test_db -f test_db_backup.sqlKey in your password and press ENTER to continue.
Your database should now be restored from either of the backup files.
You can log in to the database server again to check if the
table has been recreated.customers$ sudo -u postgres psqlEnter your password and press ENTER to proceed. Then switch to the
.test_dbpostgres=# \c test_db;Try querying data from the
tablecustomerstest_db=# SELECT * FROM customers;[Need a solution to another query? We are just a click away.]
Conclusion
Today, we saw steps followed by our Support Engineers to configure vultr postgresql