A quick reference to PostgreSQL.
Created on: 2019-01-22
Tag: cheat_sheet
To check postgresql version:
sudo -u postgres psql -c "SELECT version();"
To create a user:
sudo -u postgres createuser $user_name
To create a super suer with passowrd:
sudo -u postgres createuser -slPW $user_name
To reset password of a users:
sudo -u postgres psql -c "ALTER USER \"$psql_username\" WITH PASSWORD '$psql_password'"
To create a database on Postgresql:
sudo -u postgres createdb -E UTF8 -O $psql_user $database
To create an extension:
create extension $extension_name
To remove an extension:
drop extension $extension_name
To check postgresql version:
sudo -u postgres psql -c "SELECT version();"
To backup a psql database:
pg_dump -h $host -U username -d database > database.dump
To backup a psql database without typing password 2:
PGPASSWORD="mypass" pg_dump -h localhost -p 5432 -U username -Fc -b -v -f dumpfilename.dump databasename OR PGPASSWORD="password" pg_dump -h localhost -p 5432 -U $user -Fc -b -v -f dumpfilename.dump $database
To restoring a psql database:
psql -h $host -U $psql_user -d $database < database.back OR pg_restore -h $host -U $psql_user -d $database < database.back
To create database from sql file:
psql $database < infile.sql
To vacuum database psql:
psql -h $host $database -U $psql_user VACUUM ANALYZE;
to backup a single table:
pg_dump -h $host -p 5432 -U $psql_user -d $database -t $table_name > backup.sql
to revoke user access from a database 3:
REVOKE ALL PRIVILEGES ON DATABASE "$database" from $username;
to export a sql statement output to a csv file with header:
Copy (<sql_statement>) To '/var/lib/postgresql/$file_name.csv' DELIMITER ',' CSV HEADER;
to export a sql statement output to a geojson file:
copy (<sql_statement_to_show_output_as_json>) to '/var/lib/postgresql/$file_name.geojson';
COPY current_relation_members TO '/var/lib/postgresql/csv/current_relation_members.CSV' DELIMITER ',' CSV HEADER;
The following are the Meta-Commands for psql command. This can be used with either with -c flag of the psql command like:
sudo -u postgres psql -c "$META_COMMAND"
Or within the interactive prompt that comes after sudo -u postgres psql command. There are many Meta-Commands and there are available in the psql document. Bellow are a few useful most useful for me:
connect to a database: \c $DATEBASE_NAME
list all users: \du
list all database: \l or \list
list all table: \dt
To allow remote connections to PostgreSQL database server, first check listen_addresses in postgresql.conf:
grep listen /etc/postgresql/$PGSQL_VERSION/main/postgresql.conf
The output would show something like this:
listen_addresses = 'localhost' # what IP address(es) to listen on;
Now let's edit the postgresql.conf file in our editor of choice:
sudo vim /etc/postgresql/$PGSQL_VERSION/main/postgresql.conf
Search for listen_addresses, and set it to '*' for all addresses or comma separated IP address, save the file and exit. Now we need to change the pg_hba.conf file so let's open it with our editor:
sudo vim /etc/postgresql/$PGSQL_VERSION/main/pg_hba.conf
More details here PostgreSQL: Documentation: Connections and Authentication.
Now add the following to the end of file:
host all all 0.0.0.0/0 md5
Now save the file and exit. Now restart PostgreSQL:
/etc/init.d/postgresql restart
OR:
sudo systemctl status postgresql@$PGSQL_VERSION-main.service
source: https://bosnadev.com/2015/12/15/allow-remote-connections-postgresql-database-server/
Now connect to the remote server:
psql -h hostname -U username -d database
source: https://askubuntu.com/a/423181