Contents
Overview
Command Line Options
* Connection options:
-h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port -U, --username=USERNAME user name to connect as -w, --no-password never prompt for password -W, --password force password prompt
DB Management
Start/Stop
cd C:\pgsql\bin # Start pg_ctl start -D C:\pgsql\data # Stop pg_ctl stop -D C:\pgsql\data # Restart pg_ctl restart -D C:\pgsql\data # Status pg_ctl status -D C:\pgsql\data
Create/Remove DB
* Check if user has permission to create db
SELECT usecreatedb FROM pg_user WHERE usename='pguser';
* Create:
CREATE DATABASE dbname [ WITH [ LOCATION = 'dbpath' ] [ TEMPLATE = template ] [ ENCODING = encoding ] ] # Example: CREATE DATABASE pguser2 WITH OWNER pguser TEMPLATE template0 ENCODING 'UTF8'; CREATE DATABASE dvdrental;
* Drop:
DROP DATABASE dbname;
# Example:
DROP DATABASE pguser2;
DROP DATABASE dvdrental;
SQL Client: psql
* Syntax:
psql [option...] [dbname [username]] psql --help
* Examples:
psql --host=localhost --username=pguser
Restore DB
* Using pg_restore
# Create DB psql --host=localhost --username=pguser CREATE DATABASE dvdrental; # Import dvdrental pg_restore -U pguser -d dvdrental C:\temp\pgsql\dvdrental.tar
* Using pgAdmin
User Management
ALTER USER pguser PASSWORD 'pguser';
Tips
SQL Developer
username: testuser password: mypass Host: 127.0.0.1:1234/testdb? # The ? is important Port: 1234
References
* Practical PostgreSQL by John C. Worsley; Joshua D. Drake
* PostgreSQL Tutorial