PostgreSQL CheatSheet
21 May 2018
Find config file location:
psql -U postgres -c 'SHOW config_file'
# /etc/postgresql/12/main/postgresql.conf
Install postgress 11 for ubuntu
- Source : https://www.postgresql.org/download/linux/ubuntu/
- Run the following commands :
# create file /etc/apt/sources.list.d/pgdg.list and add the following line
deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
apt-get install postgresql-11
Istall PostGIS for postgres 11 :
- Source : http://postgis.net/install/
- Run the following commands :
vi /etc/apt/sources.list
# add the following lite at the end of the file :
deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-cache policy postgis #gives list of version
sudo apt install postgis #install default version
# create a DB with postgis support
sudo -u postgres psql
CREATE DATABASE gisdb;
\connect gisdb;
CREATE SCHEMA postgis;
ALTER DATABASE gisdb SET search_path=public, postgis, contrib;
connect gisdb; -- this is to force new search path to take effect
CREATE EXTENSION postgis SCHEMA postgis;
SELECT postgis_full_version();
Install and use Postgresql9.6 from console
-
Links: YUM Installation: https://wiki.postgresql.org/wiki/YUM_Installation First Steps: https://wiki.postgresql.org/wiki/First_steps
-
Install: To list available packages:
yum list postgresql*
For example, to install a basic PostgreSQL 9.6 server:yum install postgresql96-server
-
Data directory for database will be at:
/var/lib/pgsql96/data
-
Initialize service:
service postgresql96 initdb
-
Set PostgreSQL start automatically when OS starts:
chkconfig postgresql96 on
- Control service
To control the database service, use:
service <name> <command>
wherecan be: start : start the database stop : stop the database restart : stop/start the database; used to read changes to core configuration files reload : reload pg_hba.conf file while keeping database running
E.g. to start version 9.6:
service postgresql-9.6 start
- Removing. To remove everything:
yum erase postgresql96*
First steps after installation:
sudo -s
- swith to root user to be able to login as postgres usersu postgres
- switch to postgres user for manipulations with DB.psql help
- check that psql command works fine and PostgreSQL is really running.
CREATE SCHEMA test;
CREATE USER ec2 PASSWORD '123456';
GRANT ALL ON SCHEMA test TO ec2;
GRANT ALL ON ALL TABLES IN SCHEMA test TO ec2;
CREATE DATABASE kishinev OWNER postgres
- \du —> List all users
- drop user –echo ec2 —> drop user
ec2
- createuser –no-superuser –no-createdb –no-createrole –echo –pwprompt ec2
Change password:
- psql
- \password
- enter new password twice
- find ‘pg_hba.conf’ file and add/update the following line: (file is here -> /var/lib/pgsql96/data/pg_hba.conf)
FROM »
local all postgres peer
TO »local all postgres md5
- restart postgresql96 service:
sudo service postgresql96 restart
- Source —> https://stackoverflow.com/questions/18664074/getting-error-peer-authentication-failed-for-user-postgres-when-trying-to-ge
PSQL commands:
1. `psql -U postgres -d test` ---> Connect as user **postgres** to database **test**
2. \du ---> list all users
3. \l OR \list ---> list all databases
4. \dt ---> list all tables
5. \dt+ ---> list extended info for all tables
6. \dt *.* ---> list all tables for all schemas
7. \dt gis.* ---> list all tables for schema 'gis'
8. \dn ---> list all schemas
9. \dn+ ---> list extended info for schemas
Dump Database:
pg_dump -U postgres test > test.sql
will create a SQL sump of database test using user postgres
Restore Database Dump:
psql -U postgres
CREATE DATABASE test OWNER postgres;
psql -U postgres test < test.sql
Enable remote access to PostgreSQL server:
- Connect to the PostgreSQL server via SSH .
- Add the following line in the end of /var/lib/pgsql/data/postgresql.conf file:
listen_addresses = '*'
- Add the following line in the end of /var/lib/pgsql/data/pg_hba.conf file:
host all all 111.222.333.4444 trust
- 111.222.333.444 is the remote IP from which connection is allowed. If you want to allow connection from any IP specify 0.0.0.0/0 .
trust
is the authentication method, which allows the connection unconditionally. As for other authentication methods refer to PostgreSQL documentation - https://www.postgresql.org/docs/9.6/auth-pg-hba-conf.html.
- Restart PostgreSQL server to apply the changes:
systemctl restart postgresql.service
psql -U postgres kishinev < kishinev.sql
How completely uninstall PostgreSQL 9.X on Mac OSX
Source: https://gist.github.com/Atlas7/b1a40a2ffd85728b33e7
If installed PostgreSQL with homebrew , enter brew uninstall postgresql
If you used the EnterpriseDB installer , follow the following step.
- Run the uninstaller on terminal window
sudo /Library/PostgreSQL/9.X/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
- If installed with Postgres Installer, do:
2.1 open /Library/PostgreSQL/9.X/uninstall-postgresql.app
- Remove the PostgreSQL and data folders. The Wizard will notify you that these were not removed.
sudo rm -rf /Library/PostgreSQL
2.2 Remove the ini file:sudo rm /etc/postgres-reg.ini
2.3 Remove the PostgreSQL user using System Preferences -> Users & Groups.- Unlock the settings panel by clicking on the padlock and entering your password.
- Select the PostgreSQL user and click on the minus button.
- Restore your shared memory settings: sudo rm /etc/sysctl.conf
How can I tell what port my postgreSQL server is running on?
- check your postgresql.conf in your $PGDATA directory
Can’t start postgres
Error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
- Issue this command and check the server.log:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
- maybe the issue with permission —>
chmod 700 /usr/local/var/postgres
- maybe there is an old version of postgres —>
–> remove /usr/local/var/postgres directory
–> create directory again, and initializee DB:
initdb /usr/local/var/postgres/ -E utf8 --no-locale
–> startpostgres:pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
- maybe the issue with permission —>