# How to install PostgreSQL via MacPorts on OSX
In order to install PostgreSQL on OSX, you need to know which versions are currently supported.
Use this command to see what versions you have available.
sudo port list | grep "^postgresql[[:digit:]]\{2\}[[:space:]]"
You should get a list that looks like the following:
postgresql10 @10.14 databases/postgresql10
postgresql11 @11.9 databases/postgresql11
postgresql12 @12.4 databases/postgresql12
postgresql13 @13.1 databases/postgresql13
postgresql80 @8.0.26 databases/postgresql80
postgresql81 @8.1.23 databases/postgresql81
postgresql82 @8.2.23 databases/postgresql82
postgresql83 @8.3.23 databases/postgresql83
postgresql84 @8.4.22 databases/postgresql84
postgresql90 @9.0.23 databases/postgresql90
postgresql91 @9.1.24 databases/postgresql91
postgresql92 @9.2.24 databases/postgresql92
postgresql93 @9.3.25 databases/postgresql93
postgresql94 @9.4.26 databases/postgresql94
postgresql95 @9.5.23 databases/postgresql95
postgresql96 @9.6.19 databases/postgresql96
Today, I'm going to use the version of 12.4.
sudo port install postgresql12-server postgresql12
We can see the installation log like this:
Some of the ports you installed have notes:
apache2 has the following notes:
For information about configuring Apache2, see
https://trac.macports.org/wiki/howto/Apache2
A startup item has been generated that will aid in starting apache2 with
launchd. It is disabled by default. Execute the following command to start
it, and to cause it to launch at startup:
sudo port load apache2
libidn has the following notes:
Please be aware that GNU libidn2 is the successor of GNU libidn. It comes
with IDNA 2008
and TR46 implementations and also provides a compatibility layer for GNU
libidn.
libpsl has the following notes:
libpsl API documentation is provided by the port 'libpsl-docs'.
mariadb-10.1 has the following notes:
On activation if no /opt/local/etc/mariadb-10.1/my.cnf file exists one
will be created which loads
/opt/local/etc/mariadb-10.1/macports-default.cnf.
If a /opt/local/etc/mariadb-10.1/my.cnf file exists MacPorts does not
touch it and any changes you make to /opt/local/etc/mariadb-10.1/my.cnf
will be preserved (e.g., during port upgrades, deactivations or
activations). /opt/local/etc/mariadb-10.1/my.cnf is a good place to
customize your mariadb-10.1 installation.
Any changes made to /opt/local/etc/mariadb-10.1/macports-default.cnf
will be lost during port upgrades, deactivations or activations so you
are advised to not make changes here. Currently
/opt/local/etc/mariadb-10.1/macports-default.cnf contains only one
directive; to disable networking. With disabled networking it is
possible to install and have running all the MacPorts mysql ports
simultaneously.
nginx has the following notes:
A set of sample configuration files has been installed in
/opt/local/share/nginx/examples.
Additionally, the files nginx.conf, mime.types, fastcgi.conf have been
copied to /opt/local/etc/nginx if they didn't exist yet.
Adjust these files to your needs before starting nginx.
A startup item has been generated that will aid in starting nginx with
launchd. It is disabled by default. Execute the following command to start
it, and to cause it to launch at startup:
sudo port load nginx
postgresql12 has the following notes:
To use the postgresql server, install the postgresql12-server port
postgresql12-server has the following notes:
To create a database instance, after install do
sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
sudo su postgres -c 'cd /opt/local/var/db/postgresql12 &&
/opt/local/lib/postgresql12/bin/initdb -D
/opt/local/var/db/postgresql12/defaultdb'
A startup item has been generated that will aid in starting
postgresql12-server with launchd. It is disabled by default. Execute the
following command to start it, and to cause it to launch at startup:
sudo port load postgresql12-server
python27 has the following notes:
To make this the default Python or Python 2 (i.e., the version run by the
'python' or 'python2' commands), run one or both of:
sudo port select --set python python27
sudo port select --set python2 python27
python37 has the following notes:
To make this the default Python or Python 3 (i.e., the version run by the
'python' or 'python3' commands), run one or both of:
sudo port select --set python python37
sudo port select --set python3 python37
python38 has the following notes:
To make this the default Python or Python 3 (i.e., the version run by the
'python' or 'python3' commands), run one or both of:
sudo port select --set python python38
sudo port select --set python3 python38
rsync has the following notes:
To use the rsyncd server you must copy /opt/local/etc/rsyncd.conf.example to
rsyncd.conf and add your modules there. See 'man rsyncd.conf' for more
information.
A startup item has been generated that will aid in starting rsync with
launchd. It is disabled by default. Execute the following command to start
it, and to cause it to launch at startup:
sudo port load rsync
Here is the key message:
sudo mkdir -p /opt/local/var/db/postgresql12/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql12/defaultdb
sudo su postgres -c 'cd /opt/local/var/db/postgresql12 && /opt/local/lib/postgresql12/bin/initdb -D /opt/local/var/db/postgresql12/defaultdb'
The files belonging to this database system will be owned by user "postgres".This user must also own the server process.
fixing permissions on existing directory /opt/local/var/db/postgresql12/defaultdb ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
All the databases installed via MacPorts are located in the following position.
ls /opt/local/var/db
mariadb-10.1 mongodb php71 postgresql12 redis
Now we start the server:
sudo port load postgresql12-server
Use ps command to check the process:
ps -ef | grep postgres
To use the psql
command we should set the environment path.
vi ~/.profile
export PATH=/opt/local/lib/postgresql12/bin:$PATH
source ~/.profile
PostgreSQL can only performed via postgres
user. Let's switch the user.
sudo su postgres
Check the version:
sh-3.2$ psql --version
psql (PostgreSQL) 12.4
Show databases:
psql -l
You will see a prompt from postgres:
sh-3.2$ psql
psql (12.4)
Type "help" for help.
postgres=#
Here you can type a query to see that the server is running.
postgres=# SELECT setting FROM pg_settings WHERE name='data_directory';
And see the response:
setting
------------------------------------------
/opt/local/var/db/postgresql12/defaultdb
(1 row)
postgres=#
Congratulations! You now have a running PostgreSQL instance on OS/X.
# Use
Create database:
createdb db_name
Choose the database:
psql db_name
Create table:
create table users (name varchar(20));
Check tables:
db_name=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
Select target table:
db_name=# \d users
Table "public.users"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
name | character varying(20) | | |
Delete table:
db_name=# drop table users;
DROP TABLE
Import sql file:
sh-3.2$ psql test
psql (12.4)
Type "help" for help.
test=# \i init.sql
CREATE TABLE
← H5页面终端适配