Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

PostgreSQL Tutorial: How To Insert Data Into A PostgreSQL Database

By Nicholas Brown

PostgreSQL databases contain relations (also referred to as ‘tables’) in which you store records. Each table contains rows and columns, and each column represents a field. For example, in a table named ‘batteries’, you could have a model column, type column, and capacity column. Each row would contain a record (information about a battery in this case). This format is very clean and conducive to data analysis. It also forms the basis of the relational model.

How To Log Into PostgreSQL In Ubuntu

How To Create A Table In PostgreSQL

How To Save Data In A PostgreSQL Table

You can now insert a record (otherwise called a row) in this PostgreSQL table. Let’s start with a lithium-ion battery, and you must use single quotes, as it is a string. Also note that if you want to create a table or column name with uppercase letters in it, you must put the name in quotes. For example: If you were to name the ‘batteries’ table ‘Batteries’, you would have to type “Batteries” whenever performing a query on it.

INSERT INTO batteries VALUES ('NCR18650B', 'Lithium-Ion', 12);

The parameters in the INSERT command above are in the exact order that they were defined using the CREATE TABLE command earlier. This means that ‘NCR18650B’ is the battery model, ‘Lithium-Ion’ is the type, and ’12’ is the capacity in Watt-hours (Wh).

You can insert records using a different order, or omit some fields by using the following method. I’ll omit the ‘type’ field:

INSERT INTO batteries (model, capacity)
VALUES ('IMR18650', 9);

All you need to do is state which fields you’re inserting the values into. View your table by typing: ‘SELECT * FROM batteries;‘ (this is a read operation/read query, which i’ll explain on another page). Your table should now look like this:

As you can see, you were able to omit the type field for the IMR18650. Let’s pretend this was done in error, and correct it using the ‘UPDATE’ and ‘WHERE’ statements in the next part of this tutorial, where will use the ‘date’ and ‘numeric’ data types. The date type helps us keep track of dates efficiently, and the numeric type enables us to store decimals.

In the next section, we’ll discuss updating records, dropping tables, and more data types.

When you’re ready, read about authentication so you can manage user roles and harden your PostgreSQL installation.

How To Backup And Restore A PostgreSQL Database

Also recommended: How to change PostgreSQL authentication method.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published