Cheat Sheet for Administrating a PostgreSQL Database/Server

PostgreSQL Interactive Terminal

 

Postgresql is similar to MySQL in that it uses an interactive terminal. To gain access type the following as root

su - postgres -c psql

You’re now logged into the Postgresql interactive terminal and interacting with the local server.

Common Shell Commands

 

Dump a database.

When dumping and restoring a database, you have to work within the postgres user, this is the default setup. The home directory for the postgres user is /var/lib/postgresql

pg_dump -U username database -f file.sql

 

Restore a database.

In order to restore the database you will need to ensure that the database name exist, for instance if it was dropped.

psql -U username -d database -f file.sql

 

Common Interactive Terminal Commands

 

Connect to a database, like “use database” in MySQL.

connect databasename;

 

View current databases on local server

select datname from pg_database;

 

View current databases on local server

\l

 

 Show current roles

select rolname from pg_roles;

 

Create a user.

create user ramesh with password 'tmppassword';

 

Create a database.

CREATE DATABASE mydb WITH OWNER ramesh;

 

Drop database.

DROP DATABASE mydb;

If you notice that you’re unable to drop a database because of connections, then run the following.

SELECT
pg_terminate_backend(procpid)
FROM
pg_stat_activity
WHERE -- don't kill my own connection!
procpid <> pg_backend_pid();

If it’s a busy database then you may need to run the following first.

REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;

Did you like this article?


0 Shares:
You May Also Like

Boeing’s unmanned A160T Hummingbird helicopter takes flight

Not the first Unmanned aerial vehicle (UAV) made. But the Boeing A160HT Humming Bird should be one of the best once further tests and simulation in combat missions is completed.
Boeing's unmanned A160T Hummingbird helicopter takes flight -

Filed under:


It's been a few ticks since we've seen Boeing send an atypical aircraft into the friendly skies, but the firm's latest helicopter has successfully completed a 12-minute test flight without so much as a pilot on board. The A160T Hummingbird unmanned rotorcraft is a turbine-powered "warfighter" that aims to provide "intelligence, surveillance, and reconnaissance coverage" in locales that could make even the most calloused veteran queasy. During its time in the air, it reportedly met every objective set for it, and while we've no idea how soon this thing will be lifting itself up, it'll eventually reach speeds of up to 140 knots and stay airborne for up to 20 hours before returning to base for a pat on the wing.

[Via The Raw Feed, image courtesy of SkyControl]

 

Read | Permalink | Email this | Comments


Office Depot Featured Gadget: Xbox 360 Platinum System Packs the power to bring games to life!

[EnGadget]
Read More