Seth Barrett

Daily Blog Post: July 1st, 2023

post

July 1st, 2023

Navigating PostGreSQL: Mastering Basic Commands and Database Interaction on Linux - Part 2

Welcome back to our series on PostGreSQL for Linux users! In our previous post, we covered the basics of setting up a PostGreSQL environment on your Linux machine. Now, we'll dive into some basic PostGreSQL commands and show you how to connect to your new database.

Step 1: Access the PostGreSQL Command Line Interface

To access the PostGreSQL command line interface, you'll need to use the psql command. Open up a terminal and enter the following command:

psql -d mydatabase -U myuser

Replace "mydatabase" and "myuser" with the name of the database and the user you created in the previous post.

Once you've entered this command, you'll be prompted to enter the password for your PostGreSQL user. After you've entered your password, you'll be connected to your PostGreSQL database.

Step 2: Create a Table

Now that you're connected to your PostGreSQL database, let's create a table to store some data. Here's an example table definition:

CREATE TABLE people (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    email VARCHAR(100)
);

Enter this command into the PostGreSQL command line interface to create a new table called "people" with four columns: "id", "name", "age", and "email".

Step 3: Insert Data into the Table

Now that you have a table, let's add some data to it. Here's an example command to insert a new row into the "people" table:

INSERT INTO people (name, age, email) VALUES ('John Doe', 30, 'john.doe@example.com');

This command will insert a new row into the "people" table with the name "John Doe", age 30, and email address "john.doe@example.com".

Step 4: Retrieve Data from the Table

To retrieve data from the "people" table, you can use the SELECT command. Here's an example command to retrieve all rows from the "people" table:

SELECT * FROM people;

This command will retrieve all rows from the "people" table and display them in the PostGreSQL command line interface.

That's it for this post! In the next post in this series, we'll cover some more advanced PostGreSQL commands and show you how to interact with your database using Python.