@Commands :
#Login to postgresql:
psql -d mydb -U myuser -W
psql -h myhost -d mydb -U myuser -W
psql -d mydb -U myuser -W
psql -h myhost -d mydb -U myuser -W
#Default Admin Login:
sudo -u postgres psql -U postgres
sudo -u postgres psql -U postgres
#List databases on postgresql server:
psql -l [-U myuser] [-W]
psql -l [-U myuser] [-W]
#Turn off line pager pagination in psql:
\pset pager
\pset pager
#Determine system tables:
select * from pg_tables where tableowner = 'postgres';
select * from pg_tables where tableowner = 'postgres';
#List databases from within a pg shell:
\l;
\l;
#List databases from UNIX command prompt:
psql -U postgres -l;
psql -U postgres -l;
#Describe a table:
\d tablename
\d tablename
#Quit psql:
\q;
\q;
#Switch postgres database within admin login shell:
\connect databasename;
\connect databasename;
#Reset a user password as admin:
alter user usertochange with password 'new_passwd';
alter user usertochange with password 'new_passwd';
#Show all tables:
\dt;
\dt;
#List all Schemas:
\dn;
\dn;
#List all users:
\du;
\du;
#Load data into posgresql:
psql -W -U username -H hostname < file.sql
psql -W -U username -H hostname < file.sql
#Dump (Backup) Data into file:
pg_dump -W -U username -h hostname database_name > file.sql
pg_dump -W -U username -h hostname database_name > file.sql
#Increment a sequence:
SELECT nextval('my_id_seq');
SELECT nextval('my_id_seq');
#Create new user:
CREATE USER tom WITH PASSWORD 'myPassword';
CREATE USER tom WITH PASSWORD 'myPassword';
#Change user password:
ALTER USER Postgres WITH PASSWORD 'mypass';
ALTER USER Postgres WITH PASSWORD 'mypass';
#Grant user createdb privilege:
ALTER USER myuser WITH createdb;
ALTER USER myuser WITH createdb;
#Create a superuser user:
create user mysuper with password '1234' SUPERUSER
create user mysuper with password '1234' SUPERUSER
# or even better
create user mysuper with password '1234' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION;
create user mysuper with password '1234' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION;
#Upgrade an existing user to superuser:
alter user mysuper with superuser;
alter user mysuper with superuser;
# or even better
alter user mysuper with SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION
alter user mysuper with SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION
#Show Database Version:
SELECT version();
SELECT version();
#Change Database Owner:
alter database database_name owner to new_owner;
alter database database_name owner to new_owner;
#Copy a database:
CREATE DATABASE newdb WITH TEMPLATE originaldb;
CREATE DATABASE newdb WITH TEMPLATE originaldb;
#View Database Connections:
SELECT * FROM pg_stat_activity;
SELECT * FROM pg_stat_activity;
#View show data directory (works on 9.1+; not on 7.x):
show data_directory;
show data_directory;
#Show run-time parameters:
show all;
select * from pg_settings;
show all;
select * from pg_settings;
#If you wish to list all tables, you must use:
\dt *.*;
\dt *.*;
#connect to the database, then list the tables:
\c liferay;
\dt;
\c liferay;
\dt;
#You can combine those two commands onto a single line, if you prefer:
\c liferay \dt;
\c liferay \dt;