Database

How to Install and Use PostgreSQL: A Beginner's Guide to Basic Operations

 

PostgreSQL, which is a powerful open-source relational database management system (RDBMS). PostgreSQL is known for its reliability, robustness, and support for advanced features.

  1. Installation:
    • You can install PostgreSQL on various operating systems including Linux, macOS, and Windows.
    • You can download the installation files from the official PostgreSQL website: https://www.postgresql.org/download/
    • On Ubuntu/Linux:
    • sudo apt update
      sudo apt install postgresql postgresql-contrib
      
    • Start and enable PostgreSQL service:
    • sudo systemctl start postgresql
      sudo systemctl enable postgresql
      
      or 
      
      sudo systemctl start postgresql.service
    • On Windows:
    • Download the installer from: https://www.postgresql.org/download/windows/
    • Run the installer and follow the prompts.
    • Set a password for the default user postgres.
    • Install pgAdmin if you want a GUI.

       

  2. Setup:
    • During installation, PostgreSQL will prompt you to set a password for the default database superuser (postgres). Remember this password as it's crucial for managing your PostgreSQL installation.
    • After installation, ensure that the PostgreSQL service is running.

 

Command Line Tools:

  • Access PostgreSQL from Terminal
  • For Linux/macOS:
    • Login as the default user:
    • Switch to the Postgres user (if required):

      sudo -i -u postgres
      
      or 
      
      sudo su postgres(user)
      [sudo] password for user: 
    • Open the PostgreSQL interactive terminal (psql):

      psql
    • You should see something like:

      postgres=#
      
      
  • On Windows or Mac (if a password is set):
  • PostgreSQL must be added to your system's PATH during installation. If not, you can manually add the bin directory (e.g., C:\Program Files\PostgreSQL\16\bin) to PATH.
  • psql -U postgres
    
  • -U postgres specifies you're connecting as the default user postgres.
  • It will prompt you for a password (the one you set during installation).
  • PostgreSQL provides command-line tools for database administration and management. The primary tool is psql, which is a terminal-based front end to PostgreSQL.
  • You can access psql by opening your terminal/command prompt and typing psql.

Other information related to postgesql

Connect To the postgres server

psql -U postgres_username  -h database_host -p port_number    # to connect to server database

psql -U postgres_username -d database_name -h database_ip_address -p post_number  # connect server

psql -U user_name -d database_name; # connect with username and database name

psql -U username; # connect through username

Create a New User and Grant Permissions

CREATE USER myuser WITH PASSWORD 'mypassword';

# provide the privileges to the particular user to the particular database
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

#revoke the privileges
REVOKE ALL PRIVILEGES ON DATABASE mydb FROM blogadmin;

SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE grantee = 'amrit';

SELECT * FROM pg_roles WHERE rolname = 'amrit';


--If you want amrit to have role-level privileges like creating DBs:

ALTER ROLE amrit CREATEDB;
-- or make it a superuser
ALTER ROLE amrit SUPERUSER;

ALTER ROLE amrit NOSUPERUSER;



You can similarly revoke other attributes:

AttributeRevoke Command
Create DBALTER ROLE amrit NOCREATEDB;
Create RoleALTER ROLE amrit NOCREATEROLE;
LoginALTER ROLE amrit NOLOGIN;

 

 

 

 

Additional commands 

drop user user_name;
drop role user_name;  # to delete the user 


create user user_name superuser; #to create useras superuserwhich have all access right
 
alter user user_name with password 'new_password';

SELECT rolname FROM pg_roles;

SQL:

  • SQL (Structured Query Language) is the language used to interact with relational databases.
  • Learn SQL syntax, including how to create databases, tables, insert data, retrieve data using SELECT statements, update and delete data, and perform basic aggregation functions.
  • There are many online resources and tutorials available to learn SQL.

 

Data Types:

  • PostgreSQL supports various data types such as integer, text, varchar, date, timestamp, boolean, etc. Familiarize yourself with these data types and their usage.

 

Constraints:

  • PostgreSQL allows you to enforce data integrity using constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, etc.

 

Indexes:

  • Indexes are used to improve the performance of database queries. Learn how to create indexes on tables based on your query patterns.

 

Transactions and Concurrency:

  • Understand the concepts of transactions, isolation levels, and concurrency control in PostgreSQL.

 

Advanced Features:

  • Once you're comfortable with the basics, explore advanced features such as stored procedures, triggers, views, user-defined types, and user-defined functions.
CommandDescription
\lList all databases
\c your_databaseConnect to a specific database
\dtList all tables in the current database
\d table_namedescribe the table

\du

or

SELECT * FROM pg_catalog.pg_roles;

List all users (to see all the database users and roles)
\qQuit psql terminal
psql -U username -d database_nameconnect yo database with particular user
  
  

 

Advance Commands

1.To move the local file to the database

psql -U username -d data_base_name -f local_database_file.sql

it will move the local_database_file.sql to the postgres database (you need to crate the databse first to move in server)

 

2.Downlaod the server database to the local

pg_dump -U user_name -h host_ip_address -p db_port -d server_database_name > database_name_you_want_dump.sql

it will download the server_database_name database in your local with name database_name_you_want_dump


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top