Wednesday, May 6, 2020

PSQL Commands

To connect to postgresql using psql, update port, dbname, user and your_password in cmd below
psql "host=localhost port=5432 dbname={your_database} user=user password={your_password} sslmode=require"

To import data dump from .sql file using psql, use following cmd
psql > \i 'path-to-dump.sql'
Having single quote around the path is important else you may see C:: permission denied error. If using sudo 
sudo -u postgres psql db_name < 'file_path'  
Create user 
postgres=# create user user_name password 'password';  
Create database 
postgres=# create database mydb owner user_name ;
Install PostgreSQL Client on Ubuntu
bash:=# apt install postgresql-client- ;
To Quit
bash:=# \q;
Help
bash:=# \h;
To describe attributes of a table.
bash:=# \d tablename ;
To get list of databases.
bash:=# \l ;
To connect to a database.
bash:=# \c databasename ;
To list all details of a database.
bash:=# \z  or select * from pg_tables where schemaname = 'information_schema';

No comments:

Post a Comment