--- layout: post status: publish published: true title: 'PostgreSQL 101' author: display_name: 'sipp11' date: '2013-10-26' categories: - database tags: postgresql 101 --- I found that PostgreSQL works differently to MySQL. PostgreSQL will accept command from both command shell ($) and PostgreSQL shell (postgres=#). When you create database, drop database, you can do from command shell: $ createdb -E UTF8 $ dropdb while [charset support listed here](http://www.postgresql.org/docs/8.4/static/multibyte.html#MULTIBYTE-CHARSET-SUPPORTED). or you can create database inside postgres shell. $ psql postgres=# create database postgres=# drop database To do some basic query, it's pretty like MySQL. However, it's vastly different when it comes to list/describe postgres=# \l # show databases postgres=# \connect # use database =# \d # show tables =# \d # show columns =# \d+ # describe table Configure for remote connection ### Server side Edit `/etc/postgres/x.x/main/postgresql.conf` listen_addresses = '*' Add line to `/etc/postgres/x.x/main/pg_hba.conf` host all all 0.0.0.0/0 md5 or you could do things like `192.168.1.1/24` if you want to limit to your subnet. Creating remote user isn't anything different from what local user is: $ psql template1 template1=# create user with password ''; template1=# \connect ; => GRANT USAGE ON SCHEMA public to ; => GRANT SELECT ON ALL TABLES IN SCHEMA public TO ; => GRANT UPDATE ON ALL TABLES IN SCHEMA public TO ; => GRANT ALL ON ALL TABLES IN SCHEMA public TO ; => ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ; => GRANT USAGE, SELECT ON SEQUENCE _id_seq TO ; => GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA TO ; => \q ### Client side Pretty much like MySQL $ psql -h -d -U If you don't have postgresql client yet # apt-get install postgresql-client-x.x while x.x means version, e.g. `postgresql-client-9.1`