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

Exim4 and PHP and PHP-CGI mail() function using incorrect From: and applying Sender: headers.

If you're using Exim4 and PHP as a module or as a CGI with suexec. You may have noticed some issues with your mail. Specifically you would have noticed that either the "From:" header was using "nobody@machinename" or "user@machine name, its also possibly that you had an additional header called "Sender:". There are two things you need to do to fix this. You first need to make sure that your "php.ini" has the following value "sendmail_path = /usr/sbin/sendmail -t -i". Which is the default, double check this variable as it might be set to something else.
Read More

Want a Google Reader equivalent, then check out Tiny Tiny RSS 1.2.12

When I firsted looked at the this application, I thought it was going to look ugly and have no functionallity. I mean after looking at Google Reader and using it for some time, it really was another good Google Application. And it was made to make it easy to read and sort RSS feeds. One look at the demo, and I was hooked. It has some common features that Google Reader does, starred items and labels. I just wonder if you can plop and authentication/user account creation on it so that you can offer your own branded reader similar to Googles. TinyTiny RSS 1.2.12 Demo!
Tiny Tiny RSS 1.2.12 (Default branch) - Screenshot Tiny Tiny RSS is a Web-based news (RSS, RDF, or Atom) feed aggregator designed to allow you to read news from any location, while feeling as close to a real desktop application as possible.
License: GNU General Public License (GPL)
Changes:
This release fixes reported bugs, adds the zh_CN translation, and adds various minor improvements.

[FreshMeat]
Read More