sipp11
10 years ago
5 changed files with 227 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
||||||
|
<meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}.html" /> |
||||||
|
</head> |
||||||
|
</html> |
@ -0,0 +1,71 @@ |
|||||||
|
--- |
||||||
|
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 <database-name> -E UTF8 |
||||||
|
$ dropdb <database-name> |
||||||
|
|
||||||
|
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 <database-name> |
||||||
|
postgres=# drop database <database-name> |
||||||
|
|
||||||
|
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 <database-name> # use database |
||||||
|
<database-name>=# \d # show tables |
||||||
|
<database-name>=# \d <table-name> # show columns |
||||||
|
<database-name>=# \d+ <table-name> # 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 <user-name> with password '<your-password>'; |
||||||
|
template1=# \connect <database-name>; |
||||||
|
<database-name>=> GRANT USAGE ON SCHEMA public to <user-name>; |
||||||
|
<database-name>=> GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user-name>; |
||||||
|
<database-name>=> GRANT UPDATE ON ALL TABLES IN SCHEMA public TO <user-name>; |
||||||
|
<database-name>=> GRANT ALL ON ALL TABLES IN SCHEMA public TO <user-name>; |
||||||
|
<database-name>=> ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO <user-name>; |
||||||
|
<database-name>=> GRANT USAGE, SELECT ON SEQUENCE <table_name>_id_seq TO <user-name>; |
||||||
|
<database-name>=> GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA <schema_name> TO <user-name>; |
||||||
|
<database-name>=> \q |
||||||
|
|
||||||
|
### Client side |
||||||
|
|
||||||
|
Pretty much like MySQL |
||||||
|
|
||||||
|
$ psql -h <postgres-server> -d <database-name> -U <user-name> |
||||||
|
|
||||||
|
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` |
@ -0,0 +1,25 @@ |
|||||||
|
--- |
||||||
|
layout: post |
||||||
|
status: publish |
||||||
|
published: true |
||||||
|
title: 'Python: Performance Benchmark' |
||||||
|
author: |
||||||
|
display_name: 'sipp11' |
||||||
|
date: '2013-11-20' |
||||||
|
categories: |
||||||
|
- coding |
||||||
|
tags: python benchmark |
||||||
|
--- |
||||||
|
|
||||||
|
`[item for sublist in l for item in sublist] is faster than the shortcuts posted so far. |
||||||
|
|
||||||
|
For evidence, as always, you can use the timeit module in the standard library: |
||||||
|
|
||||||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]' |
||||||
|
10000 loops, best of 3: 143 usec per loop |
||||||
|
|
||||||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])' |
||||||
|
1000 loops, best of 3: 969 usec per loop |
||||||
|
|
||||||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x, y: x+y,l)' |
||||||
|
1000 loops, best of 3: 1.1 msec per loop |
Loading…
Reference in new issue