DEV Community

Andreas Bergström
Andreas Bergström

Posted on

Quick and Handy Tips for Everyday PostgreSQL Tasks

PostgreSQL is an advanced, open-source Object-Relational Database Management System (ORDBMS) that offers a plethora of features and performance benefits. In this blog post, we will cover six quick and handy tips for everyday tasks in PostgreSQL, which can help streamline your workflow and make your life as a developer or database administrator much easier. Let's dive in!

Check if Date is Today or Later

When working with date and time values in PostgreSQL, you might want to filter records based on whether the timestamp is today or later. To achieve this, you can use the following query:

SELECT * FROM your_table_name
WHERE any_timestamp >= now()::date;
Enter fullscreen mode Exit fullscreen mode

This query will return all rows where the any_timestamp column is today or later.

Reset Auto Increment Counter of a Column

There may be times when you need to reset the auto-increment counter of a serial column in PostgreSQL. You can do this by using the setval function. The following query demonstrates how to reset the auto-increment counter for the 'users_id_seq' sequence to 5:

SELECT setval('users_id_seq', 5);
Enter fullscreen mode Exit fullscreen mode

Find the Size of a Database

It's essential to keep an eye on your database's size to ensure optimal performance and manage storage effectively. PostgreSQL offers a simple query to find the size of a database, as shown below:

SELECT pg_size_pretty(pg_database_size('your_database_name'));
Enter fullscreen mode Exit fullscreen mode

This query returns the size of the specified database in a human-readable format.

Find the Size of a Table:

Similarly, you can find the size of a specific table in PostgreSQL using the following query:

SELECT pg_size_pretty(pg_total_relation_size('your_table_name'));
Enter fullscreen mode Exit fullscreen mode

This query returns the size of the specified table in a human-readable format.

Delete Duplicate Rows from a Table:

Duplicate rows in a table can lead to inaccurate results and performance issues. To delete duplicate rows from a table in PostgreSQL, you can use the following query:

DELETE FROM your_table_name
WHERE your_primary_key_column IN (
    SELECT your_primary_key_column
    FROM (
        SELECT your_primary_key_column,
               ROW_NUMBER() OVER (PARTITION BY column1, column2, column3 ORDER BY your_primary_key_column) AS row_num
        FROM your_table_name
    ) t
    WHERE t.row_num > 1
);
Enter fullscreen mode Exit fullscreen mode

This query will delete all duplicate rows based on the specified columns, keeping only the first unique row.

Change the Owner of a Table

In some cases, you might need to change the owner of a table in PostgreSQL. This can be achieved using the following query:

ALTER TABLE your_table_name
OWNER TO new_owner_name;
Enter fullscreen mode Exit fullscreen mode

This query changes the owner of the specified table to the new owner's name.

These quick and handy tips can save you time and effort when working with PostgreSQL. Whether you're checking dates, resetting auto-increment counters, finding the size of databases and tables, deleting duplicate rows, or changing table owners, these tips will help you perform everyday tasks more efficiently. Keep them in your toolkit, and you'll be well on your way to mastering PostgreSQL!

Top comments (0)