0% found this document useful (0 votes)
46 views5 pages

Postgresql CMD For General Tasks

The document provides instructions for managing PostgreSQL user credentials, including creating users, granting privileges, and connecting to the database. It also outlines commands for backing up and restoring databases, tables, and schemas, along with various administrative tasks. Additionally, it includes examples of SQL commands for user management and database operations.

Uploaded by

sekarann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views5 pages

Postgresql CMD For General Tasks

The document provides instructions for managing PostgreSQL user credentials, including creating users, granting privileges, and connecting to the database. It also outlines commands for backing up and restoring databases, tables, and schemas, along with various administrative tasks. Additionally, it includes examples of SQL commands for user management and database operations.

Uploaded by

sekarann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

========================================================

operations user credentials and creating script


======================

CREATE USER operations WITH PASSWORD 'HJy654EDFV';

GRANT CONNECT on DATABASE c4h_db to operations;

GRANT USAGE on SCHEMA public to operations;

GRANT SELECT,insert,update,delete on ALL TABLES in schema c4h_rc to operations;

ALTER USER operations WITH LOGIN;


---------------------------
Postgresql Connection string
----------------------
psql -h 10.3.3.5 -p 5432 -U postgres -d postgres

========
General
==============

1. Connect to PostgreSQL:

psql -U <username> -d <database_name> -h <hostname> -p <port>

2. Create a Database:

CREATE DATABASE dbname;

3. Create a User:

CREATE USER username WITH PASSWORD 'password';

4. Grant Privileges:

GRANT ALL PRIVILEGES ON DATABASE dbname TO username;

5. List Databases:

\l
6. List Tables:

-- Set path:

SET search_path to c4h_rc

\dt
7. Describe a Table:

\d table_name

8. Backup Database:

pg_dump -U <username> -h <hostname> -p <port> -d <database_name> > backup.sql


9. Restore Database:

psql -U <username> -h <hostname> -p <port> -d <database_name> < backup.sql

10. Check for Long Running Queries:

SELECT * FROM pg_stat_activity;

11. Show Current Connections:

SELECT * FROM pg_stat_activity;

12. Show Server Version:

SELECT version();

13. View Logs:

tail -f /var/log/postgresql/postgresql.log

=================================
Creating and grant user access:
------ ------------------

1. Create a User with Superuser Privileges:

CREATE USER username WITH PASSWORD 'password' SUPERUSER;

2. Create a User with Specific Permissions:

CREATE USER username WITH PASSWORD 'password' CREATEDB CREATEROLE;

3. Alter User Password:

ALTER USER username WITH PASSWORD 'new_password';

4. Grant Connect Privilege:

GRANT CONNECT ON DATABASE dbname TO username;

5. Grant Usage on Schema:

GRANT USAGE ON SCHEMA schema_name TO username;

6. Grant Select, Insert, Update, Delete on Table:

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE table_name TO username;

7. Grant All Privileges on Table:

GRANT ALL PRIVILEGES ON TABLE table_name TO username;

8. Grant Execute Privilege on Function:


GRANT EXECUTE ON FUNCTION function_name(arg1, arg2) TO username;

9. Grant Role to User:

GRANT rolename TO username;

10. Revoke Privileges:

REVOKE ALL PRIVILEGES ON TABLE table_name FROM username;

11. Revoke Connect Privilege:

REVOKE CONNECT ON DATABASE dbname FROM username;

12. List User Roles:

\du

13. List User Permissions:

\dp table_name

14. Show User's Current Role:

SHOW ROLE;

15. Grant Temporary Privileges:

GRANT TEMPORARY, CONNECT ON DATABASE dbname TO username;

16. Grant Default Privileges:

ALTER DEFAULT PRIVILEGES IN SCHEMA schema_name GRANT SELECT, INSERT ON TABLES TO


username;

17. Grant Privileges to All Tables in a Schema:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO username;

18. Grant Privileges to All Sequences in a Schema:

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO username;

-- Optional: Grant additional privileges if needed, such as sequences, functions,


etc.
-- GRANT USAGE, EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO your_user;
-- GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO your_user;

-- Optional: Grant privileges on specific tables, if needed


-- GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE your_table TO your_user;

-- Optional: Grant privileges on specific columns, if needed


-- GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE your_table TO your_user;

-- Optional: Grant privileges on specific sequences, if needed


-- GRANT USAGE, SELECT ON SEQUENCE your_sequence TO your_user;
===================

============
Database Backup:

==================
Backup Entire Database:

pg_dump -U <username> -h <hostname> -p <port> -d <database_name> > backup.sql

Backup Specific Table:

pg_dump -U <username> -h <hostname> -p <port> -d <database_name> -t <table_name> >


table_backup.sql

Backup Schema:

pg_dump -U <username> -h <hostname> -p <port> -d <database_name> -n <schema_name> >


schema_backup.sql

Backup with Compression:

pg_dump -U <username> -h <hostname> -p <port> -d <database_name> | gzip >


backup.sql.gz

--Base Backup:
---------------
pg_basebackup -U <username> -h <hostname> -p <port> -D /path/to/backup
This creates a base backup of the entire PostgreSQL data directory.

Database Restore:
==================
Restore Entire Database:

psql -U <username> -h <hostname> -p <port> -d <database_name> < backup.sql

Restore Specific Table:


psql -U <username> -h <hostname> -p <port> -d <database_name> -c "TRUNCATE
<table_name>;" && psql -U <username> -h <hostname> -p <port> -d <database_name> <
table_backup.sql

Restore Schema:

psql -U <username> -h <hostname> -p <port> -d <database_name> -c "DROP SCHEMA IF


EXISTS <schema_name> CASCADE; CREATE SCHEMA <schema_name>;" && psql -U <username> -
h <hostname> -p <port> -d <database_name> < schema_backup.sql

Restore from Compressed Backup:

gunzip -c backup.sql.gz | psql -U <username> -h <hostname> -p <port> -d


<database_name>

==============

You might also like