Since I don't use PostgreSQL as often as MySQL, I tend to forget simple commands for administering the database. Hopefully these notes will help as reference when working with PostgreSQL:
-
Login as "postgres" (SuperUser) to start using database:
# su - postgresCreate a new database:
$ createdb mydbDrop database:
$ dropdb mydbAccess database:
$ psql mydbGet help:
mydb=# \hQuit:
mydb=# \qRead command from file:
mydb=# \i input.sqlTo dump a database:
$ pg_dump mydb > db.outTo reload the database:
$ psql -d database -f db.outDump all database:
# su - postgres # pg_dumpall > /var/lib/pgsql/backups/dumpallRestore database:.sql
# su - postgres # psql -f /var/lib/pgsql/backups/dumpallShow databases:.sql mydb
#psql -l or mydb=# \l;Show users:
mydb=# SELECT * FROM "pg_user";Show tables:
mydb=# SELECT * FROM "pg_tables";Set password:
mydb=# UPDATE pg_shadow SET passwd = 'new_password' where usename = 'username';Clean all databases (Should be done via a daily cron):
$ vacuumdb --quiet --all
Related Reading:
- sandip's blog
- Login or register to post comments
Comments
plpgsql development tips
Turn on stats_block_level in "postgresql.conf" first, then run the below query:
SELECT datname, )$'
blks_read,
blks_hit,
round((blks_hit::float / (blks_read+blks_hit+1) * 100)::numeric, 2) as cachehitratio
FROM pg_stat_database
WHERE datname !~ '^(template(0|1)|postgres
ORDER BY cachehitratio desc;
Reference: free space map
As a very rough guide start with "max_fsm_pages" set to the sum of relpages in pg_class:
select sum(relpages) from pg_class;
To enable autovacuum, stats_start_collector and stats_row_level must be enabled in "postgresql.conf":
autovacuum = on
stats_start_collector = on
stats_row_level = on
Reference: autovacuum
Turn on stats_command_string in "postgresql.conf" first, then run the below query:
select * from pg_stat_activity;
Additional Reference: viewing current postgresql queries
show all;
for i in `psql -t -c "select datname from pg_catalog.pg_database"`; do echo $i; psql -t -c "select pg_size_pretty(pg_database_siz e('$i'))"; done
DROP TABLE name [, ...] [ CASCADE ]
DROP TABLE removes tables from the database. Only its owner may destroy a table. To empty a table of rows, without destroying the table, use DELETE.
DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified. CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.
Reference:
OnLamp
Add the below line to /var/lib/pgsql/data/postgresql .conf to make postgresql database listen to external connections:
listen_addresses = '*'
Edit /var/lib/pgsql/data/pg_hba.con f and add the appropriate permissions:
host all all 0.0.0.0/0 md5
template1=# ALTER DATABASE <dbname> OWNER TO <dbuser>;
Edit "/var/lib/pgsql/data/pg_hba.co nf" with:
local all &nb sp; all &nb sp; &nb sp; &nb sp; &nb sp; &nb sp; md5 sp; all &nb sp; 127.0.0.1/32 md5
host all &nb
local - socket connection
host - tcp connection
To connect via socket:
$ psql -U <username> -d <dbname>
To connect via tcp:
$ psql -U <username> -h <hostname> -d <dbname>
template1=# ALTER USER <dbuser> WITH PASSWORD '<password>';
\d -- view the owner
\dp -- view permissions
Create db user and grant permission to create databases.
# adduser <dbuser>
# su - postgres
$ createuser -d -S -R <dbuser>