Simple CRUD

1. Open the PostgreSQL CLI

  • Launch psql in your terminal:

    Copy

    psql -U postgres
  • Connect to your database (replace your_database with the name of your database):

    Copy

    \c your_database

2. Create the City Table

Execute the following SQL to create the table:

Copy

CREATE TABLE City (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    country VARCHAR(255) NOT NULL,
    population INTEGER
);

3. Insert Data (CREATE)

Insert some sample data into the City table:

Copy


4. Read Data (READ)

  • Retrieve all records:

    Copy

  • Retrieve specific records:

    Copy


5. Update Data (UPDATE)

  • Update the population of a city:

    Copy

  • Verify the update:

    Copy


6. Delete Data (DELETE)

  • Delete a record:

    Copy

  • Verify the deletion:

    Copy


7. Drop the Table (Optional)

If you want to remove the City table:

Copy


Last updated