Postgresql: Difference between revisions
Jump to navigation
Jump to search
Line 56: | Line 56: | ||
su postgres -c "/usr/pgsql-9.5/bin/pg_archivecleanup /var/lib/pgsql/9.5/archivedir/ $(basename $(find /var/lib/pgsql/9.5/archivedir/ -ctime +2 | tail -n 1))" | su postgres -c "/usr/pgsql-9.5/bin/pg_archivecleanup /var/lib/pgsql/9.5/archivedir/ $(basename $(find /var/lib/pgsql/9.5/archivedir/ -ctime +2 | tail -n 1))" | ||
==restore database from backup== | ==restore database from backup== | ||
createdb -T template0 '${dbname}' | |||
gunzip -c ${filename.gz} | psql ${dbname} | gunzip -c ${filename.gz} | psql ${dbname} | ||
=bookmarks= | =bookmarks= | ||
https://wiki.postgresql.org/wiki/Disk_Usage | https://wiki.postgresql.org/wiki/Disk_Usage |
Revision as of 14:21, 2 April 2020
psql
run command from cli
psql -c "SELECT datname FROM pg_database;"
Connect to remote database:
export PGPASSWORD=password && psql --host=10.111.222.1 --port=5492 --username=user --no-password postgres
install psql
apt-get install freetds-bin
List databases
Alternative 1
SELECT datname FROM pg_database;
Alternative 2
gives more information too.
\l Name | Owner | Encoding | Collate | Ctype | Access privileges -------------+-------------+----------+-------------+-------------+----------------------- indata_mdb | uc_etl_prod | UTF8 | sv_SE.UTF-8 | sv_SE.UTF-8 | ...
select database
\c testdb;
show tables
\dt
Change view. One line per attribute
\x on
exit/quit
\q
update
UPDATE users SET admin = 1 WHERE user_id = 14002;
Create database
CREATE DATABASE dbname OWNER username;
Delete database
DROP DATABASE dbname;
delete database from cli
dropdb $dbname
Import a database
psql username -h hostname -d dbname < dump.sql
Create user
CREATE USER username WITH PASSWORD 'MYPASS';
Set super privileges
ALTER USER username WITH SUPERUSER;
List users
\du
Deleting user
DROP USER nomusr
Getting help
\? or \h
size of database
SELECT pg_size_pretty( pg_database_size('database') );
size of table
SELECT pg_size_pretty( pg_total_relation_size('tablename') );
clean archives older than 2 days
su postgres -c "/usr/pgsql-9.5/bin/pg_archivecleanup /var/lib/pgsql/9.5/archivedir/ $(basename $(find /var/lib/pgsql/9.5/archivedir/ -ctime +2 | tail -n 1))"
restore database from backup
createdb -T template0 '${dbname}' gunzip -c ${filename.gz} | psql ${dbname}