Skip to content

Instantly share code, notes, and snippets.

@skuppa
Last active March 28, 2016 19:53
Show Gist options
  • Save skuppa/8909590 to your computer and use it in GitHub Desktop.
Save skuppa/8909590 to your computer and use it in GitHub Desktop.
Postgres Tips

JDBC Driver

Name Value
artifact postgresql:postgresql:8.4-702.jdbc3
driver org.postgresql.Driver
JDBC url jdbc:postgresql://localhost:5432/testdb

Connecting Heroku Postgres

Use ssl to true and sslfactory as NonValidatingFactory

JDBC Url: jdbc:postgresql://ec2-xx.xx.xx.xx.compute-1.amazonaws.com:5432/testdb?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

Drop schema without dropping the database

drop schema public cascade;
create schema public;
@skuppa
Copy link
Author

skuppa commented Oct 31, 2014

Install postgres

Install postgres
brew update
brew install postgres
Initialize the database
initdb /usr/local/var/postgres -E utf8
Store socket for connecting via socke
sudo mkdir /var/pgsql_socket/
ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
Connecting for first time
psql -d template1
create user postgres

@skuppa
Copy link
Author

skuppa commented Nov 8, 2014

Install Postgres on Mac OS server Maverics 10.9

Stop postgres

sudo su
serveradmin stop postgres

Configuration file location

vim /Library/Server/PostgreSQL/Config/org.postgresql.postgres.plist

Move old postgres data directory

mv /Library/Server/PostgreSQL/Data /Library/Server/PostgreSQL/DataOld_1

Create and Initialize new postgres data directory

sudo -u _postgres /Applications/Server.app/Contents/ServerRoot/usr/bin/initdb /Library/Server/PostgreSQL/Data -E utf8 --lc-collate=C --lc-ctype=C

Import old data into new postgres data (Optional)

sudo -u _postgres /Applications/Server.app/Contents/ServerRoot/usr/bin/pg_upgrade -b /Applications/Server.app/Contents/ServerRoot/usr/libexec/postgresql9.2 -B /Applications/Server.app/Contents/ServerRoot/usr/bin -d /Library/Server/PostgreSQL/DataOld_1 -D /Library/Server/PostgreSQL/Data

Load the service and restart the postgres service

launchctl load -w /Applications/Server.app/Contents/ServerRoot/System/Library/LaunchDaemons/org.postgresql.postgres.plist
serveradmin start postgres

Login to template database

The default user is "_postgres"

sudo psql -U _postgres -d template1

Create user and granting create database privilege

create user postgres with password 'postgres';
alter user postgres createdb;
\q

Create mido database

psql -U postgres -h localhost
create database mido;

Unload the launchtl

launchctl remove org.postgresql.postgres

Logs file directory

/Library/Logs/PostgreSQL/PostgresSQL.log

@skuppa
Copy link
Author

skuppa commented Feb 18, 2015

String aggregation

select t.id, count(t.id), string_agg(cast(t.id as text), ',') from ticket group by t.id having count(t.id) > 1

@skuppa
Copy link
Author

skuppa commented Mar 28, 2016

Staring the database

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Checking the status

pg_ctl -D /usr/local/var/postgres status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment