markdown PSQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown PSQL相关的知识,希望对你有一定的参考价值。

### Check for POSTGRESQL
* check for latest version: `postgres --version`
  * if command not found: `brew install postgres`
* To have launchd start postgresql now and restart at login: 
`brew services start postgresql`
  * Or, if you don't want/need a background service you can just run:
  `pg_ctl -D /usr/local/var/postgres start`
* To view other version of psql: `brew search postgresql`
  * To install a version: `brew install postgresql@#`
* Make sure you run either start commands and not both!
* If you run the `pg_ctl` command and you get an error about the user database not installed
  * run (within the service): `createdb 'whoami'`: with tick marks, not single quotes!
* Check to see if it runs: `psql`
  * `psql` utility function that lets you perform admin functions  
---
### For a clean install
* `brew doctor`
* `brew update`
* `brew uninstall postgres`
* `rm -rf rm -rf /usr/local/var/postgres`
* `rm -rf .psql_history .psqlrc .psql.local .pgpass .psqlrc.local`
* `brew list` to confirm postgres is actually deleted
* `brew update`
* `brew install postgres`
### Rails with PSQL
* **MAKE SURE TO `brew install postgresql` BEFORE BUNDLING IF YOU HAVEN'T INSTALLED
POSTGRESQL YET!!**
* `rails new app_name -T -d postgresql`
### Configurations
* Default user is postgres
* Enter the console by: `psql postgres`
* See all the users: `\du`
* If missing the postgres user because you installed Postgres
**via homebrew** (in terminal): `/usr/local/opt/postgres/bin/createuser -s postgres`
* Set up password: `psql -U postgres`
  * `\password postgres`
  * follow prompts

---
### Create new User
* log in as postgres user: `psql -U postgres` OR `psql postgres`
  * With a user: `psql postgres -U nameofuser` 
* create a database: `CREATE DATABASE name;` **don't forget the semicolon!!!***
* create a user: `CREATE ROLE nameofuser WITH PASSWORD '';`
* Grant priviliges: `GRANT ALL PRIVILEGES ON DATABASE nameofDB TO nameofuser;`
* Grant only create db privilege: `ALTER ROLE name CREATEDB;`
* `\q` quits
* `\list` lists all databases
* `\dt` lists all tables in the database
* [Link to documentation on how to postgres](https://www.codementor.io/engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb)

以上是关于markdown PSQL的主要内容,如果未能解决你的问题,请参考以下文章

markdown Mysql到Psql

markdown PSQL片段

markdown psql查询到文件 - OUTPUT QUERY,EXPORT QUERY

markdown psql如何使用SSH隧道远程服务器中的数据库?

执行多个查询时,“psql -c”和“psql -f”有啥区别?

如何解决这个问题以使用 psql? | psql:错误:致命:角色“postgres”不存在